做一个 HTTPPOST MVC2
我有一个 MVC2 应用程序。在此应用程序中,我有一个与模型 NewHorseModel 竞争的强类型视图:
public class NewHorseModel
{
public List<Category> Faehigkeit { get; set; }
}
public class Choice
{
public int Id { get; set; }
public string Name { get; set; }
public string Beschreibung { get; set; }
public bool Selected { get; set; }
}
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public string Beschreibung { get; set; }
public List<Category> Subcategories { get; set; }
public List<Choice> Choices { get; set; }
public int Parent { get; set; }
}
视图如下所示:
<p>
<input faehigkeit_ID="1" id="Selected" name="Selected" type="checkbox" value="true" />
<input name="Selected" type="hidden" value="false" />
Mitteltrab
<input id="Id" name="Id" type="hidden" value="1" />
</p>
<p>
<input faehigkeit_ID="2" id="Selected" name="Selected" type="checkbox" value="true" />
<input name="Selected" type="hidden" value="false" />
Arbeitstrab
<input id="Id" name="Id" type="hidden" value="2" />
</p>
<p>
<input faehigkeit_ID="3" id="Selected" name="Selected" type="checkbox" value="true" />
<input name="Selected" type="hidden" value="false" />
Trab versammelt
<input id="Id" name="Id" type="hidden" value="3" />
</p>
<p>
<input faehigkeit_ID="11" id="Selected" name="Selected" type="checkbox" value="true" />
<input name="Selected" type="hidden" value="false" />
Trab
<input id="Id" name="Id" type="hidden" value="11" />
</p>
视图的创建方式如下: 类别和子类别MVC2
现在我想做一篇文章,但如何获取数据? 当我发表这样的帖子时:
[HttpPost]
public void myAction(NewHorseModel newHorseModel)
{
// ...
}
NewHorseModel 中的 Faehigkeit 为 null
这是我的 ASPX 代码:
<div id="Div2">
<%
foreach (Category item2 in Model.Faehigkeit)
{
Html.RenderPartial("Faehigkeit", item2);
}
%>
</div>
部分视图类别(强类型模型类别):
<%
if (Model.Choices != null)
{
foreach (var item in Model.Choices)
{
Html.RenderPartial("Choice", item);
}
}
if (Model.Subcategories != null)
{
foreach (var item in Model.Subcategories)
{
Html.RenderPartial("Faehigkeit", item);
}
}
%>
以及部分视图选择(强类型模型选择)
<p>
<%: Html.CheckBoxFor(m => m.Selected, new { faehigkeit_ID = Model.Id }) %>
<%: Model.Name %>
<%: Html.HiddenFor(u=>u.Id) %>
</p>
更新 下一个测试: 在 Faehigkeit.ascx 部分中,我添加了以下代码:
<input type="hidden" name="Faehigkeit[<%=Model.Id%>].Id" value="<%=Model.Id%>" />
<input type="hidden" name="Faehigkeit[<%=Model.Id%>].Name" value="<%: Model.Name%>" />
在 Choices.ascx 部分中,我添加了以下代码:
<input type="checkbox" name="Faehigkeit[0].Choices[<%=Model.Id%>].Selected" />
我不需要知道哪个选择属于哪个类别。我必须知道哪个选择 ID 被检查,哪个没有检查。
HTML 输出如下所示:
<input type="hidden" name="Faehigkeit[1].Id" value="1" />
<input type="hidden" name="Faehigkeit[1].Name" value="Qualität der Gangarten" />
<input type="hidden" name="Faehigkeit[1].Choices[4].Id" value="4" />
<input type="checkbox" name="Faehigkeit[1].Choices[4].Selected" />
我的控制器如下所示: [HttpPost]
public ActionResult CreateNewHorse(NewHorseModel collection)
{
if (User.Identity.IsAuthenticated)
{
return View();
}
else
{
return View("Account/LogOn");
}
}
如果我尝试获取“Faehigkeit”的值 -> “选择”一切都是空(“Faehigkeit”的名称,“Faehigkeit”的ID,并且没有选择 显示调试期间 NewHorseModel 内容的图像: https://i.sstatic.net/6yLZW.png
非常感谢!
I have a MVC2-application. in this application I have a strongtyped view contending the modell NewHorseModel:
public class NewHorseModel
{
public List<Category> Faehigkeit { get; set; }
}
public class Choice
{
public int Id { get; set; }
public string Name { get; set; }
public string Beschreibung { get; set; }
public bool Selected { get; set; }
}
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public string Beschreibung { get; set; }
public List<Category> Subcategories { get; set; }
public List<Choice> Choices { get; set; }
public int Parent { get; set; }
}
The View looks like this:
<p>
<input faehigkeit_ID="1" id="Selected" name="Selected" type="checkbox" value="true" />
<input name="Selected" type="hidden" value="false" />
Mitteltrab
<input id="Id" name="Id" type="hidden" value="1" />
</p>
<p>
<input faehigkeit_ID="2" id="Selected" name="Selected" type="checkbox" value="true" />
<input name="Selected" type="hidden" value="false" />
Arbeitstrab
<input id="Id" name="Id" type="hidden" value="2" />
</p>
<p>
<input faehigkeit_ID="3" id="Selected" name="Selected" type="checkbox" value="true" />
<input name="Selected" type="hidden" value="false" />
Trab versammelt
<input id="Id" name="Id" type="hidden" value="3" />
</p>
<p>
<input faehigkeit_ID="11" id="Selected" name="Selected" type="checkbox" value="true" />
<input name="Selected" type="hidden" value="false" />
Trab
<input id="Id" name="Id" type="hidden" value="11" />
</p>
The view is created like in this post:
Categories and Subcategories MVC2
now I want to do a post, but how to get the datas?
When I do a post like this:
[HttpPost]
public void myAction(NewHorseModel newHorseModel)
{
// ...
}
Faehigkeit in NewHorseModel is null
Here my ASPX Code:
<div id="Div2">
<%
foreach (Category item2 in Model.Faehigkeit)
{
Html.RenderPartial("Faehigkeit", item2);
}
%>
</div>
The partial view Category (strong typed model Category):
<%
if (Model.Choices != null)
{
foreach (var item in Model.Choices)
{
Html.RenderPartial("Choice", item);
}
}
if (Model.Subcategories != null)
{
foreach (var item in Model.Subcategories)
{
Html.RenderPartial("Faehigkeit", item);
}
}
%>
And the partialview Choices (strongtyped model choice)
<p>
<%: Html.CheckBoxFor(m => m.Selected, new { faehigkeit_ID = Model.Id }) %>
<%: Model.Name %>
<%: Html.HiddenFor(u=>u.Id) %>
</p>
Update
Next test:
in the Faehigkeit.ascx partial I have added this code:
<input type="hidden" name="Faehigkeit[<%=Model.Id%>].Id" value="<%=Model.Id%>" />
<input type="hidden" name="Faehigkeit[<%=Model.Id%>].Name" value="<%: Model.Name%>" />
in the Choices.ascx partial I have added following code:
<input type="checkbox" name="Faehigkeit[0].Choices[<%=Model.Id%>].Selected" />
I don't need to know which choice is in wich category. I kust need to know which choice ID is checked and which on not.
The HTML-Output looks like this:
<input type="hidden" name="Faehigkeit[1].Id" value="1" />
<input type="hidden" name="Faehigkeit[1].Name" value="Qualität der Gangarten" />
<input type="hidden" name="Faehigkeit[1].Choices[4].Id" value="4" />
<input type="checkbox" name="Faehigkeit[1].Choices[4].Selected" />
My controler looks like this:
[HttpPost]
public ActionResult CreateNewHorse(NewHorseModel collection)
{
if (User.Identity.IsAuthenticated)
{
return View();
}
else
{
return View("Account/LogOn");
}
}
If I try to get the value of "Faehigkeit" -> "Choices" Every thing is Null (the Name of the "Faehigkeit", the ID of "Faehigkeit", and there are no choices
An image that shows the content of the NewHorseModel during debuging:
https://i.sstatic.net/6yLZW.png
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我写了 2 篇博客文章,将解决您的问题:
解释了如何将
IList
发布到控制器操作此处 将从头到尾指导您,以便您了解它实际上是如何进行的作品和为什么。由于您可能在客户端有复杂的 JSON 对象,并且希望它们在您的操作方法中在服务器上进行模型绑定(如您的情况),这篇文章 可能也是一篇有趣的读物。它解释了发送复杂 JSON 对象的问题,并提供了一个简单的 jQuery 插件,该插件将客户端对象转换为一种可以轻松将数据绑定到强类型操作方法参数的形式。
根据您的代码,
您只提供了代码的某些部分(以及一些并不真正相关的代码,但无论如何),我将对您的案例进行观察。
您希望在服务器上绑定模型的所有输入字段都需要具有正确的名称。从呈现的复选框名称我们可以看到事实并非如此。
根据您的模型,您的输入应命名为(不必介意输入类型,因为只有您知道应呈现哪些输入以及应呈现哪种类型):
基于此复杂的分层模型,此表单可能会很大。您可能不希望发送所有属性,因为您只需要一些相关的属性。但最主要的是输入命名应该如所描述的那样。
如果您将数据作为 JSON 传输到客户端,则客户端上会有一个对象,如下所示:
如果您的客户端视图直接操作该对象,您可以随后通过 jQuery Ajax 调用将其发送回服务器即:
所以选择是你的,但无论哪种情况,字段名称都应该正确,否则你的模型将不会绑定到你的参数,你将无法使用它。
您可以尝试使用
<%= Html.EditorFor(m => m); %>
但这可能不是您想要使用的形式。在这种情况下,必须使用手动输入命名。或者您也可以编写一些自定义代码。其中一种方法是将部分视图模式类型更改为例如
Tuple
。 String 会告诉它什么string
应该预先添加到您的字段名称中。然后,当您继续进行时,应始终添加以前的数据,以便您的子类别列表及其相关子对象将具有正确的输入表单名称。
不太好,但我想这是唯一的方法。
There are 2 blog posts that I've written and will address your problem:
How to post
IList<T>
to controller actions is explained here and will guide you from start to finish so you'll understand how it actually works and why.And since you may have have complex JSON objects on the client side and would like them to be model bound on the server in your action method (as is in your case), this post may be as well an interesting read. It explains the problem of sending complex JSON objects and provides a simple jQuery plugin that converts client objects into a form that will easily be data bound to your strong type action method parameters.
Based on your code
You've only provided some parts of your code (and some that aren't really relevant but anyway) and I'm going to make an observation for your case.
All input fields that you wish to model bind on the server need to have correct names. From the rendered checkbox names we can see that is not the case at all.
Based on your model, your inputs should be named as (never mind input types because only you know which ones should be rendered and as which type):
Based on this complex hierarchical model this form can be huge. And you probably don't wish to send all properties because you only need some that are relevant. But the main thing is that input naming should be as described.
If you'd transfer your data as JSON to the client, you'd have an object on the client as:
And if your client side view manipulated this object directly you could then afterwards send it back to the server by means of jQuery Ajax call ie:
So the choice is yours, but in either case, field names should be correct otherwise your model won't be bound to your parameter and you won't be able to use it.
You could try with
<%= Html.EditorFor(m => m); %>
but that may not be the kind of form you'd like to use. In such case manual input naming would have to be used. Or you can as well write some custom code.One of the ways would be to chaneg your partial views mode types to become for instance
Tuple<string, Category>
. String would tell it whatstring
should be pre-pent to your field names.And then when you go along, previous data should be always added so your subcategories' lists and their respected subobjects will have correct input form names.
Not nice, but I suppose it's the only way to make it.
通常,如果您将 HttpPost 作为操作的属性,则类似于:
然后将类类型作为您的参数,它应该为您自动绑定它,如果您需要做任何疯狂的事情,或者从外部非 ASP 页面发布到此操作,您可以仅使用 FormCollection(我认为)作为您的参数,这是一个包含所有元素和值的美化字典。
Usually if you put the HttpPost as an attribute of your action, its something like:
Then put the class type as your argument it should autobind it for you, if there is anything crazy that you need to do, or are posting from an external non ASP page to this action you can just use the FormCollection (i think) as your argument and thats a glorified Dictionary containing all your elements and values.