Asp.net 如何在HttpPost方法中获得确认列表(IEnumerable)?
在我的ASP.NETMVC2应用程序中,用户会看到一个项目列表,必须单击“确认”才能将该列表持久化到数据库中 当用户单击confirm时,在HttpPost方法中,参数为null。在调用HttpPost方法之前,一切都正常。此时,必须持久化的列表为空 如何获得确认值 我已经尝试在HttpGet方法中使用TempData,但是在HttpPost方法中TempData也是空的 这是控制器代码Asp.net 如何在HttpPost方法中获得确认列表(IEnumerable)?,asp.net,asp.net-mvc,Asp.net,Asp.net Mvc,在我的ASP.NETMVC2应用程序中,用户会看到一个项目列表,必须单击“确认”才能将该列表持久化到数据库中 当用户单击confirm时,在HttpPost方法中,参数为null。在调用HttpPost方法之前,一切都正常。此时,必须持久化的列表为空 如何获得确认值 我已经尝试在HttpGet方法中使用TempData,但是在HttpPost方法中TempData也是空的 这是控制器代码 public ActionResult Confirm() { List&l
public ActionResult Confirm()
{
List<ConfirmVehicleModel> vehicles = GetAllVehicles();
return View(vehicles);
}
[HttpPost]
public ActionResult Confirm(List<ConfirmVehicleModel> model)
{
//model is null, why ?
UploadVehiclesModelService service = new Models.UploadVehiclesModelService();
service.StoreVehicles(model, User.Identity.Name);
return RedirectToAction("Index", "UploadVehicles");
}
public ActionResult Confirm()
{
列出车辆=GetAllVehicles();
返回视图(车辆);
}
[HttpPost]
公共行动结果确认(列表模型)
{
//模型为空,为什么?
UploadVehicleModelService=新车型。UploadVehicleModelService();
服务.存储车辆(型号,用户.标识.名称);
返回操作(“索引”、“上传车辆”);
}
以下是确认视图:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<RM.Application.Models.ConfirmVehicleModel>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Confirm
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
Confirm that the vehicles below should be added.</h2>
<table>
<tr>
<th>
ReferenceType
</th>
<th>
ReferenceName
</th>
</tr>
<% foreach (var item in Model)
{ %>
<tr>
<td>
<%= Html.Encode(item.ReferenceType) %>
</td>
<td>
<%= Html.Encode(item.ReferenceName) %>
</td>
</tr>
<% } %>
</table>
<div>
<% using (Html.BeginForm())
{ %>
<input type="submit" value="Confirm" />
|
<%= Html.ActionLink("Back to upload form", "Index") %>
<% } %>
</div>
</asp:Content>
证实
确认应添加以下车辆。
参考类型
参考名称
|
谢谢你的帮助
问候
Bob您的HTML.BeginForm()不合适,它应该围绕您希望传递的值
尝试:
确认应添加以下车辆。
参考类型
参考名称
item.ReferenceType)%>
item.ReferenceName)%%>
|
您的HTML.BeginForm()不合适,它应该围绕您希望传递的值
尝试:
确认应添加以下车辆。
参考类型
参考名称
item.ReferenceType)%>
item.ReferenceName)%%>
|
尼古拉斯的建议起了作用。我用for循环替换了foreach循环,并使用了
Html.HiddenFor
<% for (int i = 0; i < Model.Count(); i++)
{ %>
<%= Html.HiddenFor(model=>model[i].ReferenceType) %>
<%= Html.HiddenFor(model=>model[i].ReferenceName) %>
<tr>
<td>
<%= Html.Encode(Model[i].ReferenceType) %>
</td>
<td>
<%= Html.Encode(Model[i].ReferenceName) %>
</td>
</tr>
<% } %>
模型[i]。参考类型)%>
模型[i]。参考名称)%>
我还将视图的顶行更改为(以前是IEnumerable,现在是List)
\\
现在已填充
HttpPost
方法的列表
模型参数。Nicholas advice帮助。我用for循环替换了foreach循环,并使用了Html.HiddenFor
<% for (int i = 0; i < Model.Count(); i++)
{ %>
<%= Html.HiddenFor(model=>model[i].ReferenceType) %>
<%= Html.HiddenFor(model=>model[i].ReferenceName) %>
<tr>
<td>
<%= Html.Encode(Model[i].ReferenceType) %>
</td>
<td>
<%= Html.Encode(Model[i].ReferenceName) %>
</td>
</tr>
<% } %>
模型[i]。参考类型)%>
模型[i]。参考名称)%>
我还将视图的顶行更改为(以前是IEnumerable,现在是List)
\\
HttpPost
方法的列表
模型参数现在已填充。你好,Nicholas,感谢您的回复。我按照你的建议移动了Html.BeginForm。参数列表模式仍然为空。我还尝试将FormCollection作为参数,但该参数也是null。我需要使用标签元素吗?表单元素如何知道要回发哪些数据?当做Bob@bobentelect-您可以使用Html.HiddenForHi Nicholas将值作为隐藏值“嵌入”到表单中,谢谢您的回复。我按照你的建议移动了Html.BeginForm。参数列表模式仍然为空。我还尝试将FormCollection作为参数,但该参数也是null。我需要使用标签元素吗?表单元素如何知道要回发哪些数据?当做Bob@bobentelect-您可以使用Html.HiddenFor将值作为隐藏值“嵌入”到表单中
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<List<RM.Application.Models.ConfirmVehicleModel>>" %>\\