[英]ASP.NET MVC3: Adding Textboxes with Jquery and binding to Model
I have created a View which prompts the user to input some records with an invoice number and save them to the DB. 我创建了一个视图,提示用户输入带有发票编号的某些记录并将其保存到数据库。 Now a new requirement came in, we must be able to save 1..N invoice numbers for one record. 现在出现了一项新要求,我们必须能够保存1..N个发票编号用于一条记录。 No problem in the database, but I cant find a clean way to change the view and the model. 数据库中没有问题,但是我找不到改变视图和模型的干净方法。 As of now I'm doing this: 截至目前,我正在这样做:
<div class="editor-label">
@Html.LabelFor(x => x.InvoiceNumber1)
</div>
<div class="editor-field">
@Html.TextBoxFor(x => x.InvoiceNumber1)
@Html.ValidationMessageFor(x => x.InvoiceNumber1)
</div>
To satisfy the new requirement I'd have to create a button of some kind which lets the user add additional textboxes (via qjuery) to the view. 为了满足新的要求,我必须创建某种类型的按钮,该按钮允许用户向视图中添加其他文本框(通过qjuery)。 But how would I bind those dynamically added textboxes back to the model on the form submit? 但是,如何将那些动态添加的文本框绑定回表单提交模型上?
Suppose your model is as follows: 假设您的模型如下:
// model
public class InvoiceModel
{
public List<int> InvoiceNumbers { get; set; }
}
Now if you name your inputs InvoiceNumbers[0]
, InvoiceNumbers[1]
etc., the values will be correctly bound to the InvoiceNumbers
property of the model. 现在,如果您将输入InvoiceNumbers[0]
, InvoiceNumbers[1]
等,则这些值将正确绑定到模型的InvoiceNumbers
属性。
Check http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx and http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/ you use Sequential Indices but to be able to remove, or add rows using javacript you can use Non-Sequential Indices as explained in both links. 检查http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx和http://blog.stevensanderson.com/2010/01/28/editing-a-variable -length-list-aspnet-mvc-2-style /您可以使用顺序索引,但是要使用javacript删除或添加行,可以使用非顺序索引,如两个链接中所述。 Regards 问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.