了解 MVC 中的表单:如何从列表填充模型<>

发布于 2024-09-27 10:00:26 字数 1927 浏览 1 评论 0 原文

图像中的结论,可以在底部找到


我在了解表单在 MVC 中的工作方式时遇到了一些麻烦(因为我是一名 WebForms 开发人员,并且真的想开始在一个大项目中使用 MVC)

我获取MVC2 Web 项目并向其中添加一个简单的ViewModel

namespace TestForms.Models
{
    public class myFormViewModel
    {
        public myFormViewModel() { this.Options = new List<myList>(); }

        public string Name { get; set; }
        public string Email { get; set; }

        public List<myList> Options { get; set; }
    }

    public class myList
    {
        public myList() { this.Value = this.Name = ""; this.Required = false; }

        public string Name { get; set; }          
        public string Value { get; set; }      
        public string Type { get; set; }
        public bool Required { get; set; }
    }
}

创建一个强类型视图,将一个新对象传递给该视图并运行它。

当我按下提交时,它不会返回 Options 部分中的内容...我怎样才能绑定它

我的观点

替代文本 http://www.balexandre.com/temp/2010- 10-11_1357.png

填写生成的表单

替代文本 http: //www.balexandre.com/temp/2010-10-11_1353.png

当我按提交时,选项部分不会传递给模型! 我忘记了什么?

alt text http:// www.balexandre.com/temp/2010-10-11_1352.png



结论

更改 View 循环来分配序号,我们现在的

<%= Html.TextBox("model.Options[" + i + "].Value", option.Value)%>

model 是我们传递给视图的模型变量的名称 OptionsList 类型的属性名称 然后我们使用属性名称

alt text

替代文字

Conclusion in Images, can be found in the bottom


I'm having some trouble to get how Forms work in MVC (as I'm a WebForms Developer and really wanna start using MVC in one big project)

I took the MVC2 Web Project and add a simple ViewModel to it

namespace TestForms.Models
{
    public class myFormViewModel
    {
        public myFormViewModel() { this.Options = new List<myList>(); }

        public string Name { get; set; }
        public string Email { get; set; }

        public List<myList> Options { get; set; }
    }

    public class myList
    {
        public myList() { this.Value = this.Name = ""; this.Required = false; }

        public string Name { get; set; }          
        public string Value { get; set; }      
        public string Type { get; set; }
        public bool Required { get; set; }
    }
}

Created a Strongly Typed View, passed a new object to the view and run it.

When I press submit, it does not return what's in the Options part... how can I bind that as well?

my view

alt text http://www.balexandre.com/temp/2010-10-11_1357.png

filling up the generated form

alt text http://www.balexandre.com/temp/2010-10-11_1353.png

when I press Submit the Options part is not passed to the Model! What am I forgetting?

alt text http://www.balexandre.com/temp/2010-10-11_1352.png



Conclusion

Changing the View loop to allocate the sequential number, we now have

<%= Html.TextBox("model.Options[" + i + "].Value", option.Value)%>

model is the name of our Model variable that we pass to the View
Options is the property name that is of type List
and then we use the property name

alt text

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘦怹 2024-10-04 10:00:26

查看您的用户界面,您似乎没有将选项成员中的数据放入其中。

<% foreach (myList obj in Model.Options) { %>
     // Add the object to your UI. they will be serialized when the form is submitted
<% } %>

另请检查您是否将数据包含在表单元素中

编辑:

抱歉!我没有意识到你正在填充控制器内的对象。您能显示视图中的代码吗?

Looking at your UI it seems that you did not put the data from the Options member on it.

<% foreach (myList obj in Model.Options) { %>
     // Add the object to your UI. they will be serialized when the form is submitted
<% } %>

Also check that you enclose the data in a form element

EDIT:

Sorry! I did'nt realized that you was filling the object inside the controller. Can you please show the code you have in the view?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文