asp.net-mvc – POST操作方法中强类型的ViewModel仅包含空值
发布时间:2020-05-24 14:33:42 所属栏目:asp.Net 来源:互联网
导读:我正在尝试使用强类型视图实现我的Edit操作方法,该视图接收自定义形状的ViewModel类.换句话说,我想要一个强类型的ViewModel,它包含应该编辑的 Linq实体以及应该在View中显示的一些其他对象. 我可以在调用GET Edit操作方法时看到该视图,但强类型的POST操作方法
|
我正在尝试使用强类型视图实现我的Edit操作方法,该视图接收自定义形状的ViewModel类.换句话说,我想要一个强类型的ViewModel,它包含应该编辑的 Linq实体以及应该在View中显示的一些其他对象. 我可以在调用GET Edit操作方法时看到该视图,但强类型的POST操作方法只接收带有null参数的ViewModel类,我无法弄清楚如何检索POST参数. View Model看起来如下: //my custom-shaped ViewModel
public class CustomersFormViewModel
{
public SelectList AccountTypesDropDownBox;
public SelectList CountriesDropDownBox;
public Customer Customer;
}
action方法如下所示: //
// GET: /CustomersController/Edit
public ActionResult Edit(int ID)
{
var model = new CustomersFormViewModel
{
Customer = repository.Load(ID.Value),CountriesDropDownBox = GetCountries(),AccountTypesDropDownBox = GetAccountTypes()
};
return View(model);
}
//
// POST: /CustomersController/Edit
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(CustomersFormViewModel model)
{
//THE NEXT LINE THROWS!!!
Debug.Assert(Model.Customer!=null);
return View(model);
}
这是我的编辑视图: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/CustAdminMaster.master"
Inherits="System.Web.Mvc.ViewPage<Zeiterfassung.Controllers.CustomersController+CustomersFormViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
NewEdit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
NewEdit</h2>
<%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm())
{%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="FirstName">FirstName:</label>
<%= Html.TextBox("FirstName",Model.Customer.FirstName) %>
<%= Html.ValidationMessage("FirstName","*") %>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List","Index") %>
</div>
</asp:Content>
我还尝试了一个带有formValues参数的POST动作方法,但是viewmodel仍然没有包含已发布的参数: [AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int ID,FormCollection formValues)
{
CustomersFormViewModel model = new CustomersFormViewModel();
UpdateModel(model);
//THE NEXT LINE STILL THROWS
Debug.Assert(model.Customer!=null);
return View("NewEdit",model);
}
到目前为止,我找到的唯一方法是编写自己的代码,从FormCollection中获取已发布的参数,并相应地更新我的自定义ViewModel.但这种方法似乎有点原始.真的没有更好的办法吗? 编辑:我刚刚在视图中尝试了不同的语法,如tvanfosson所建议,但问题仍然没有改变: <label for="Customer.FirstName">FirstName:</label>
<%= Html.TextBox("Customer.FirstName") %>
<%= Html.ValidationMessage("Customer.FirstName","*") %>
解决方法您需要根据模型中的前缀命名字段.此外,您需要修改视图模型以使用属性而不是字段.默认模型绑定器仅在绑定时查看模型上的公共属性.<label for="Customer.FirstName">FirstName:</label>
<%= Html.TextBox("Customer.FirstName") %>
<%= Html.ValidationMessage("Customer.FirstName","*") %>
这样,模型绑定器就知道如何将表单参数与模型的相应组件和关联属性相关联. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-membership – 成员资格超时和会话超时
- asp.net – 我应该有多少DBContext
- asp.net-mvc-3 – 从自定义授权属性访问角色
- 写单元测试在ASP.NET Web API中使用User.Identity.Name的方
- asp.net – 如何将两个模型传递给一个View
- asp.net-mvc – HttpCache vs Singleton – MVC应用程序的最
- asp.net – Visual Studio 2010和Visual Studio 2012中的co
- .net – MVC 4中MVC 4中强类型ActionLink的语法是什么?
- asp.net-mvc-3 – 如何使用MiniProfiler与单页Web应用程序/
- .net-4.0 – 从自定义IHttpHandler调用MvcHttpHandler.Exec
推荐文章
站长推荐
- asp.net-mvc – 如何从ASP.NET MVC VIEWS文件夹访
- 如何在asp.net mvc中的html.actionlink中调用jav
- asp.net-mvc – 在.NET 3.5 ASP.NET MVC应用程序
- asp.net-mvc – 如何在asp.net mvc3项目中开始使
- DELETE语句与ASP.NET动态数据中的REFERENCE约束冲
- 在asp.net的内容页面中查找母版页内的UnorderedL
- asp.net – 如何从我的网站项目中删除未使用的CS
- asp.net-mvc – 控制器和操作方法之间丢失的会话
- asp.net-mvc-3 – 如何在MVC3上使用authorize属性
- asp.net – 有浏览器相当于IE的ClearAuthenticat
热点阅读
