什么是适当的方法从MVC4中的视图将2个不同的模型传递给控制器(Whats the Appropriate Method to pass 2 different Models to the Controller from a View in MVC4)
我正在使用SimpleCmbership的MVC4。 MVC设置了我的UserProfile表,我修改了注册视图以适应我的布局。 一切都很顺利,直到我确定我想要从我的用户那里包含一些最适合其他现有模型的附加信息。
通常,我将多个模型从View传递给Controller的方法是使用元组。 从历史上看,这对我来说非常有用,直到现在我还没有遇到任何实际问题。
我的注册表格类似于:
@model Tuple<MyNamespace.Models.RegisterModel,MyNamespace.Models.MembershipDetail> @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) { @Html.AntiForgeryToken() <table style="border-collapse: collapse; border-spacing:0px; border-width:0px; margin: 0px; width:100px; padding: 0 0 0 0px; background-color:#2e2e2e;"> <tr> <td> Profile Name </td> <td> @Html.TextBoxFor(m => m.Item2.ProfileName) </td> </tr> <tr> <td> @Html.LabelFor(m => m.Item1.UserName) </td> <td> @Html.TextBoxFor(m => m.Item1.UserName) </td> </tr> <tr> <td> @Html.LabelFor(m => m.Item1.Password) </td> <td> @Html.PasswordFor(m => m.Item1.Password) </td> </tr> <tr> <td> @Html.LabelFor(m => m.Item1.ConfirmPassword) </td> <td> @Html.PasswordFor(m => m.Item1.ConfirmPassword) </td> </tr> <tr> <td> <button type="submit" id="btnSubmitForm" value="Register">Register</button> </td> </tr> </table> @Html.Partial("_ValidationSummary",ViewData.ModelState) }
我的控制器的注册方法类似于:
[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Register(RegisterModel model, MembershipDetail member) { if (ModelState.IsValid) { try { // Do something with the data } catch(MembershipCreateUserException e) { ModelState.AddModelError("",ErrorCodeToString(e.StatusCode)); } } return View(model); }
每当我在单击Submit按钮后调试此代码时,我都会注意到返回到控制器的两个Models都是空的。 包含的字段是null,0或Empty Strings,我无法弄清楚原因。
为了增加这个谜团,如果我从视图的顶部删除元组并将其重新分配为单个模型,如下所示:
@model MyNamespace.Models.RegisterModel
并使用1 Model推断引用(m.Property等)替换代码中的元组引用(egmItem1.Property,m.Item2.Property); 然后将从TextBox助手传入的数据适当地分配给模型,并在调试代码时填充模型。
现在,我知道我可以简单地在UserProfile表中添加一些其他字段,并在我的模型中使用它们来完全缓解元组,但后来我复制了我的模式中的数据,这不是一个理想的解决方案。 请记住,尽管此处提供的示例代码仅包含第二个模型的1个元素,但它实际上将超过1个。
那么为什么在使用Tuple时不会填充模型,是否有更合适的方法来解决这个问题? 或者我不能在单个表单提交中混合模型数据???
I am using MVC4 with SimpleMembership. MVC has setup my UserProfile table and I've modified the Registration View to accomodate my layout. Everything worked great until I determined that I wanted to include some additional information from my user that best fit within the context of another already existing Model.
Typically my approach to passing more than one Model from my View to the Controller has been to employ Tuples. Historically this has worked out great for me and I've never had any real issues until now.
My Registration Form resembles this:
@model Tuple<MyNamespace.Models.RegisterModel,MyNamespace.Models.MembershipDetail> @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) { @Html.AntiForgeryToken() <table style="border-collapse: collapse; border-spacing:0px; border-width:0px; margin: 0px; width:100px; padding: 0 0 0 0px; background-color:#2e2e2e;"> <tr> <td> Profile Name </td> <td> @Html.TextBoxFor(m => m.Item2.ProfileName) </td> </tr> <tr> <td> @Html.LabelFor(m => m.Item1.UserName) </td> <td> @Html.TextBoxFor(m => m.Item1.UserName) </td> </tr> <tr> <td> @Html.LabelFor(m => m.Item1.Password) </td> <td> @Html.PasswordFor(m => m.Item1.Password) </td> </tr> <tr> <td> @Html.LabelFor(m => m.Item1.ConfirmPassword) </td> <td> @Html.PasswordFor(m => m.Item1.ConfirmPassword) </td> </tr> <tr> <td> <button type="submit" id="btnSubmitForm" value="Register">Register</button> </td> </tr> </table> @Html.Partial("_ValidationSummary",ViewData.ModelState) }
The Register Method of my Controller is similar to this:
[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Register(RegisterModel model, MembershipDetail member) { if (ModelState.IsValid) { try { // Do something with the data } catch(MembershipCreateUserException e) { ModelState.AddModelError("",ErrorCodeToString(e.StatusCode)); } } return View(model); }
Whenever I debug this code after the click of the Submit button, I note that both Models returned to the controller are empty. The contained fields are either null, 0 or Empty Strings and I cannot figure out why.
To add to this mystery, if I remove the tuple from the top of the view and re-assign it as a single Model as such:
@model MyNamespace.Models.RegisterModel
And replace the tuple references from the code (e.g. m.Item1.Property, m.Item2.Property) with the 1 Model inferred reference (m.Property and etc); then the data passed in from the TextBox helpers is appropriately assigned to the model and the model is populated when I debug the code.
Now, I know I could simply add a few other fields to the UserProfile table and use them in my Model to alleviate the Tuple altogether but then I am duplicating data in my schema which is not an ideal solution. And keeping in mind that though my sample code provided here only contains 1 element of the 2nd Model, it will actually be more than 1.
So why don't the Models get populated when using the Tuple and is there a more appropriate way to go about solving this problem? Or can I not mix Model data on a single form submission???
原文:https://stackoverflow.com/questions/23875456
相关问答
更多-
CSS:浮动div有0高度(CSS: Floating divs have 0 height)[2023-02-22]
这不是因为浮动div没有高度,这是因为浮动div不会影响父元素的大小。 您可以使用overflow风格使父元素考虑浮动元素: #outer { overflow: auto; } It's not because the floating divs doesn't have a height, it's because the floating divs don't affect the size of the parent element. You can use the overflow style ... -
将vertical-align:top添加到img规则: img { max-width: 100%; height: auto; width: auto; vertical-align:top; } jsFiddle示例 Add vertical-align:top to your img rule: img { max-width: 100%; height: auto; width: auto; vertical-ali ...
-
你可以插入一个div,在最后一个孩子之后清除浮点数。 HTML: 小提琴: http : //jsfiddle.net/Rc5J8/ If the parent container only has floating child ...
-
如果flexbox是一个选项,你可以给: display: flex; flex-wrap: wrap; wrapper ,这将修复高度 - 见下面的演示: @import url("reset.css"); @import url('https://fonts.googleapis.com/css?family=Raleway'); * { box-sizing: border-box; } body { background-color: #9E9E9E; } #wrappe ...
-
我个人喜欢来自www.ejeliot.com的同等高度栏目 http://jsfiddle.net/spacebeers/s8uLG/3/ 您将容器设置为溢出设置为隐藏,然后在每个div上添加负边距底部和相等正边距填充底部。 #container { overflow: hidden; } #container div { float: left; background: #ccc; width: 200px; margin-bottom: -2000px; padding-bottom: 2000px; ...
-
如何获得具有自动高度的容器Div和浮动div在里面?(How to get a container Div with auto Height and with floating divs inside it?)[2023-02-27]
添加这些 .msg_ok{ overflow:hidden; zoom:1; } 并删除height:auto ,这是无用的,因为高度始终是自动的(默认情况下)。 你面临的问题是如何包含浮动 add these .msg_ok{ overflow:hidden; zoom:1; } and remove height:auto, that's useless since height is always auto (by default). the issue you a ... -
除非你可以保证每列的高度(你通常不能,使用像网一样的流体介质),你不能单独使用CSS来使用浮动元素。 但是,你有选择: 使用display: table-cell : http : //jsfiddle.net/8LdQk/3/ 。 不幸的是,这在IE6或7中不起作用。 这篇博文详细介绍了它的使用可能会有所帮助。 使用JavaScript: http : //jsfiddle.net/8LdQk/5/ 。 使用Dan Cederholm的经典人造柱技巧。 You cannot do this via CSS ...
-
将包装器设置为display: table并设置要display: table-cell的列 display: table-cell (并删除float) 所以它看起来像这样: .wrapper { display: table; ... } .wrapper .column { display: table-cell; ... } Set the wrapper to display: table and set the columns to display: table ...
-
使用display: inline-block; 和vertical-align: top对齐这些。 .photo_box { background: #fff; -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; display: inline-block; position: relative; margin-bottom: 15px; bo ...
-
这里的截面高度取决于图像高度。 如果在window.load()事件上调用高度计算逻辑,它必须正常工作。 一旦你导航回主页,然后回到有问题的页面,图像被缓存,因此它工作正常。试试这个 $(window).load(function(){ $('.section').eqHeights(); }); 加载所有图像后调用窗口加载事件。 Here the height of the section will depend on the image height. If the height calculati ...