我从一篇文章中得知,我们可以通过Html.BeginForm()
传递查询字符串,所以我这样做了
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { CustomerName = "joydev" }))
但是当我发布表单时,它没有映射到我的模型数据。
public class MyViewModel
{
public string CustomerName { get; set; }
public List<Movies> movies { get; set; }
public List<Hobbies> hobbies { get; set; }
}
看我的行动
[HttpPost]
public ActionResult Index(MyViewModel oVm, string CustomerName)
{
ViewBag.Message = "Success";
return View(oVm);
}
我看到字符串CustomerName为空,oVm CustomerName
为空。只是告诉我我在哪里犯了CustomerName
没有作为查询字符串传递给action方法的错误。请指导我。感谢
有人说: - 数据作为查询字符串发布。在行动中,它可通过所有三种方法获得,即In Model,Action Parameter和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
}
查看网址http://forums.asp.net/p/2070398/5975252.aspx?p=True&t=635801707044672469
答案 0 :(得分:0)
尝试
@using (Html.BeginForm("Index", "Home", new { CustomerName = "joydev" }, FormMethod.Post, null))
检查BeginForm的过载列表。你正在使用
public static MvcForm BeginForm(
this HtmlHelper htmlHelper,
string actionName,
string controllerName,
FormMethod method,
Object htmlAttributes
)
更改<form>
标记的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"