具有多个输入的 MVC3 强类型模型
我怎样才能在一个页面中将多个输入全部输入到我的模型列表中,其中模型被定义为
public class MatrixSet
{
List<MatrixPoints> matrixPoints { get; set; }
}
public class MatrixPoints
{
double x { get; set; }
double y { get; set; }
}
我不确定在视图中使用什么来表示,所有输入矩阵点的 4 个输入字段,然后在发布时控制器将具有矩阵集类型的模型,其中包含在视图中输入的矩阵点的列表。我知道如何在不通过模型的情况下做到这一点,但我正在努力遵守最佳实践方法。我可以让每个输入字段为 @Html.TextBoxFor() ,然后假设在我的视图顶部我使用 @model Models.MatrixSet ,它只会填充 MatrixSet 中的 MatrixPoints 列表吗?
How can I have multiple inputs in a page all feed into a list of my model where the model is defined as
public class MatrixSet
{
List<MatrixPoints> matrixPoints { get; set; }
}
public class MatrixPoints
{
double x { get; set; }
double y { get; set; }
}
I am not sure what to use in the view to have say, 4 input fields which all input matrix points and then when posted the controller will have the model of type matrixset which will contain a list of the matrix points entered in the view. I know how to do this without passing the model but I am trying to adhere to best practice methods. Can I just have each input field be @Html.TextBoxFor() and then it will just fill a list of MatrixPoints in MatrixSet assuming that at the top of my view I am using @model Models.MatrixSet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
找到答案:
在这方面,您可以迭代和动态地将项目添加到模型对象,同时在发布表单时仍然保留整个模型并保留定义模型的验证。
Found the answer:
In this regard you can add items to your model objects iteratively and dynamically while still holding the entire model when the form is posted and retaining the validation from the defined model.
您必须寻找将集合绑定到 View,然后当您发布表单时,所有集合都会收集到 ActionResult 中。
这是对您有帮助的链接
http://haacked.com/archive/2008 /10/23/model-binding-to-a-list.aspx
它对我有用。
谢谢。
You have to look for binding collection to View and later when you post form all collection get collected in ActionResult.
This is the link that help you
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
It works for me.
Thanks.