How to pass in ID with Html.BeginForm()?

后端 未结 3 1738
难免孤独
难免孤独 2020-12-28 13:44

In ASP.NET MVC I\'m using the HTML helper

Html.BeginForm(\"ActionName\", \"Controller\", FormMethod.Post);

But I need to post to: /control

相关标签:
3条回答
  • 2020-12-28 13:57

    Html.BeginForm("action", "controller", new {Id = 12345})

    0 讨论(0)
  • 2020-12-28 14:11

    Matt's should work fine. If you are still passing in FormMethod.Post, though, you need to do it like this:

    Html.BeginForm("action","controller", new { Id = 12345 }, FormMethod.Post);
    

    Reversing the third and fourth parameters will result in the Id being treated as an attribute instead of a route value.

    0 讨论(0)
  • 2020-12-28 14:16
    Html.BeginForm("action", "controller", new { id = ViewBag.FileID },
    FormMethod.Post, new { id = "feedbackform" })
    

    As for the querystring, ?type=golden, I don't know how to do that. Of course, a querysting is a get, and undermines the whole purpose of FormMethod.Post. I mean, you could use FormMethod.Get, if you want querystring data, and this might be what you are looking for.

    Additionally, you can avoid html.beginform and do the querystring, get + post, manually with a form tag.

    Thirdly, if you are using the form, you can make a hidden field:

    [input type=hidden name="type" value="golden"]
    

    Then, when the submit button is pressed the value is passed properly as a form variable.

    0 讨论(0)
提交回复
热议问题