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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9660781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:21:07  来源:igfitidea点击:

sending a model in mvc3 using html.beginform

c#asp.net-mvc-3razorhtml.beginform

提问by Josh Monks

I have an HttpPost and HttpGet version of the action method Rate() :

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

http://pastebin.com/embed_js.php?i=6x0kTdK0

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

The view that HttpGet Rate() returns:

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

I'm trying to find out to send a Model through the Form to the Post method, and my thinking was that the value "model" in the submit button's tag would be the parameter to do so, however null is being passed through if i breakpoint inside of the Post method. The for loop is trying to create 5 buttons to send the proper rating.

我试图找出通过 Form 将 Model 发送到 Post 方法,我的想法是提交按钮标签中的值“model”将是这样做的参数,但是如果我传递了 null Post 方法中的断点。for 循环试图创建 5 个按钮来发送正确的评分。

Thanks

谢谢

回答by Ragesh

I think there are two things you need to fix:

我认为您需要解决两件事:

  1. The inputtag needs a nameattribute
  2. The nameattribute should be set to model.Rating
  1. input标签需要一个name属性
  2. name属性应设置为model.Rating

回答by JIA

Them model binding works on the nameattribute as @Ragesh suggested you need to specify the name attributes matching the RatingModelproperties in the view. Also note that the submit button values dont get posted to the server, there are hacks through which you can achieve that, one way is to include a hidden field.

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

Also in your provided code the loop runs six times and at the end Model.Ratingwill be equal to 5always... what are you trying to achieve?. Say for example you have a model like

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

public class MyRating{

 public string foo{get;set;}

 }

in your view

在你看来

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

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

now your controller will look like

现在你的控制器看起来像

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

hope you will get the idea

希望你能明白