Asp.net mvc 4 如何将查询字符串映射到ASP.NETMVC模型数据
我从一篇文章中听说,我们可以通过Asp.net mvc 4 如何将查询字符串映射到ASP.NETMVC模型数据,asp.net-mvc-4,Asp.net Mvc 4,我从一篇文章中听说,我们可以通过Html.BeginForm()传递查询字符串,所以我这样做了 @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { CustomerName = "joydev" })) 但是当我发布表单时,它没有映射到我的模型数据 这是我的视图模型 我看到字符串CustomerName为空,oVm CustomerName为空。只要告诉我哪里犯了错误,CustomerName没有作为查询字符串传递给ac
Html.BeginForm()
传递查询字符串,所以我这样做了
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { CustomerName = "joydev" }))
但是当我发布表单时,它没有映射到我的模型数据
这是我的视图模型
我看到字符串CustomerName为空,oVm CustomerName
为空。只要告诉我哪里犯了错误,CustomerName
没有作为查询字符串传递给action方法。请引导我。谢谢
编辑
有人说:-数据作为查询字符串发布。在操作中,它可以通过所有三种方法获得,即在模型中、操作参数和通过Request.QueryString
public ActionResult Login(LoginModel model, string returnUrl, string testparam)
{
string additionalParamValueFromQueryString = Request.QueryString["testparam"]; // Method 1
string additionalParamValueFromModel = model.testparam; // Method 2
string additionalParamValue = testparam; // Method 3
}
请参阅url,然后使用
@using (Html.BeginForm("Index", "Home", new { CustomerName = "joydev" }, FormMethod.Post, null))
检查过载列表中是否存在过载。你正在使用
public static MvcForm BeginForm(
this HtmlHelper htmlHelper,
string actionName,
string controllerName,
FormMethod method,
Object htmlAttributes
)
它更改
标记的HtmlAttributes,生成
<form action="Home/Index" CustomerName="joydev" method="POST"
生成(并将值发送到服务器)
如果CustomerName
是MyViewModel
上的一个属性,为什么需要将其作为一个单独的参数?目前认为客户名称不是视图模型的属性。请告诉我如何将带有@Html.Beginform的查询字符串传递给操作方法?请参阅我的编辑1部分。
public static MvcForm BeginForm(
this HtmlHelper htmlHelper,
string actionName,
string controllerName,
FormMethod method,
Object htmlAttributes
)
<form action="Home/Index" CustomerName="joydev" method="POST"
public static MvcForm BeginForm(
this HtmlHelper htmlHelper,
string actionName,
string controllerName,
RouteValueDictionary routeValues,
FormMethod method
)
<form action="Home/Index?CustomerName=joydev" method="POST"