gpt4 book ai didi

c# - 使用 html.beginform 在 mvc3 中发送模型

转载 作者:行者123 更新时间:2023-11-30 15:04:52 26 4
gpt4 key购买 nike

我有操作方法 Rate() 的 HttpPost 和 HttpGet 版本:

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");
}

HttpGet Rate() 返回的 View :

@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>
}
}

我试图找出通过 Form 将模型发送到 Post 方法的方法,我的想法是提交按钮标记中的值“model”将是这样做的参数,但是传递的是 null通过 if i 在 Post 方法内部设置断点。 for 循环试图创建 5 个按钮来发送正确的评分。

谢谢

最佳答案

它们的模型绑定(bind)在 name 属性上起作用,因为@Ragesh 建议您需要指定与 View 中的 RatingModel 属性相匹配的名称属性。另请注意,提交按钮值不会发布到服务器,您可以通过一些技巧来实现这一点,一种方法是包含一个隐藏字段。

此外,在您提供的代码中,循环运行六次,最后 Model.Rating 将始终等于 5 ...您想要实现什么? .比如说你有一个像

这样的模型
public class MyRating{

public string foo{get;set;}

}

在你看来

@using(Html.BeginForm("Rate","Classified", FormMethod.Post))

@Html.TextBoxFor(x=>x.foo) //use html helpers to render the markup
<input type="submit" value="Submit"/>
}

现在你的 Controller 看起来像

[HttpPost]
public ActionResult Rate(MyRating model)
{
model.foo // will have what ever you supplied in the view
//return RedirectToAction("List");
}

希望你能明白

关于c# - 使用 html.beginform 在 mvc3 中发送模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9660781/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com