Jos*_*nks 3 c# html.beginform razor asp.net-mvc-3
我有一个HttpPost和HttpGet版本的动作方法Rate():
http://pastebin.com/embed_js.php?i=6x0kTdK0
public ActionResult Rate(User user, Classified classified)
{
var model = new RatingModel
{
CurrentUser = user,
RatedClassified = classified,
};
return View(model);
}
[HttpPost]
public ActionResult Rate(RatingModel model)
{
model.RatedClassified.AddRating(model.CurrentUser, model.Rating);
return RedirectToAction("List");
}
Run Code Online (Sandbox Code Playgroud)
HttpGet Rate()返回的视图:
@model WebUI.Models.RatingModel
@{
ViewBag.Title = "Rate";
}
Rate @Model.RatedClassified.Title
@using(Html.BeginForm("Rate","Classified", FormMethod.Post))
{
for (int i = 1; i < 6; i++)
{
Model.Rating = i;
<input type="submit" value="@i" model="@Model"></input>
}
}
Run Code Online (Sandbox Code Playgroud)
我试图找出通过Form发送模型到Post方法,我的想法是提交按钮的标签中的值"模型"将是这样做的参数,但是如果我是null则通过Post方法中的断点.for循环试图创建5个按钮来发送正确的评级.
谢谢
这个模型绑定适用于name
属性,因为@Ragesh建议您需要指定RatingModel
与视图中的属性匹配的名称属性.另请注意,提交按钮值不会发布到服务器,有一些黑客可以通过它实现,一种方法是包含隐藏字段.
同样在你提供的代码中,循环运行六次,最后总是Model.Rating
等于5
......你想要实现什么?比方说,你有一个类似的模型
public class MyRating{
public string foo{get;set;}
}
Run Code Online (Sandbox Code Playgroud)
在你看来
@using(Html.BeginForm("Rate","Classified", FormMethod.Post))
@Html.TextBoxFor(x=>x.foo) //use html helpers to render the markup
<input type="submit" value="Submit"/>
}
Run Code Online (Sandbox Code Playgroud)
现在你的控制器看起来像
[HttpPost]
public ActionResult Rate(MyRating model)
{
model.foo // will have what ever you supplied in the view
//return RedirectToAction("List");
}
Run Code Online (Sandbox Code Playgroud)
希望你能得到这个想法
归档时间: |
|
查看次数: |
28488 次 |
最近记录: |