我正在使用带有剃须刀页面的.net核心,并且我有一个模型作为对象列表,如下所示:
public class Member
{
public List<User> member { get; set; }
}
public class User
{
public String fname { get; set; }
public String lname { get; set; }
}
并且我有一个表单来填充对象,如下所示:
<form asp-controller="Population" asp-action="AddMember" method="post" class="form-horizontal" role="form" style="font-family:Cairo-Bold">
<div class="new-help add-form-container">
<input asp-for="member.LastName" type="text" />
<input asp-for="member.Firstname" type="text" />
</div>
,当用户单击按钮时,用户可以动态地从页面添加对象。我为他复制的按钮div
如下所示:
<div class="row">
<div class="col-lg-12">
<a class="clone-button"><u> Add another</u></a>
</div>
</div>
$(function () {
$(".clone-button").on('click', function () {
var ele = $(this).closest('.new-help').clone(true);
$(this).closest('.new-help').after(ele);
});
});
我如何绑定表格中的文本字段,以便将提交时添加的成员列表返回给控制器?
答案 0 :(得分:1)
您应使用以下代码生成具有索引号的元素:
$(function () {
$("#add").click(function (e) {
e.preventDefault();
var i = $(".items").length;
var n = '<input type="text" class="items" name="ListItems[' + i + '].Name" />';
$("#item-list").append(n);
});
});
请参阅此链接以获取更多信息:
答案 1 :(得分:0)
我发现模型中的List字段必须以大写字母开头,否则我无法获得正确的模型绑定。
void DispatchNotificationThatServiceIsRunning()
{
_notificationIsLive = true;
RemoteViews contentView = new RemoteViews(PackageName, Resource.Layout.Notification);
contentView.SetTextViewText(Resource.Id.txt_crrentSong_notification, Activity_Player.txt_CurrentSong.Text);
contentView.SetTextViewText(Resource.Id.txt_crrentSong__artist_notification, Activity_Player.txt_CurrentArtist.Text);
notificationBuilder = new Notification.Builder(this);
Task.Delay(_countDownFirstRound).ContinueWith(t =>
{
notificationBuilder
.SetSmallIcon(Resource.Drawable.btn_icon_header)
.SetCustomContentView(contentView);
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.Notify(50, notificationBuilder.Build());
DispatchNotificationThatServiceIsRunning();//This is for repeate every 1s.
_countDownFirstRound = 50;
// Click on notification, and return to app. Only works, because _2TimerRunning is set as : "LaunchMode = LaunchMode.SingleInstance"
Intent notificationIntent = new Intent(this, typeof(Activity_Player));
notificationIntent.SetAction(Intent.ActionMain);
notificationIntent.AddCategory(Intent.CategoryLauncher);
PendingIntent contentIntent = PendingIntent.GetActivity(this, 1, notificationIntent, PendingIntentFlags.UpdateCurrent);
notificationBuilder.SetContentIntent(contentIntent);
},
TaskScheduler.FromCurrentSynchronizationContext());
}
}
创建视图
public class Member
{
public int MemberId { get; set; }
public List<User> Users { get; set; }
}
控制器:
@model AddMember.Models.Member
<form asp-action="Create" method="post">
<div class="form-group" id="item-list">
<a href="#" id="add">Add</a>
<br/>
<input type="text" asp-for="Users" class="items" name="Users[0].fname"/>
<input type="text" asp-for="Users" class="items" name="Users[0].lname" />
</div>
<input type="submit" value="Create" class="btn btn-default" />
</form>
@section Scripts {
<script>
$(function () {
$("#add").click(function (e) {
e.preventDefault();
var i = ($(".items").length) / 2;
var n = '<input type="text" class="items" name="Users[' + i + '].fname" />' +
'<input type="text" class="items" name="Users[' + i + '].lname" />'
$("#item-list").append(n);
});
});
</script>
}