Pass multiple parameters in Html.BeginForm MVC

前端 未结 2 1352
一生所求
一生所求 2020-12-14 06:19

I have something like this:

public ActionResult Create(int clubid)
{
  var club = db.Clubs.Single(c=>c.Id == clubid);
  ViewBag.Club = club;
  Competition         


        
相关标签:
2条回答
  • 2020-12-14 06:30

    There are two options here.

    1. a hidden field within the form, or
    2. Add it to the route values parameter in the begin form method.

    Edit

    @Html.Hidden("clubid", ViewBag.Club.id)
    

    or

     @using(Html.BeginForm("action", "controller",
                           new { clubid = @Viewbag.Club.id }, FormMethod.Post, null)
    
    0 讨论(0)
  • 2020-12-14 06:31

    Another option I like, which can be generalized once I start seeing the code not conform to DRY, is to use one controller that redirects to another controller.

    public ActionResult ClientIdSearch(int cid)
    {
      var action = String.Format("Details/{0}", cid);
    
      return RedirectToAction(action, "Accounts");
    }
    

    I find this allows me to apply my logic in one location and re-use it without have to sprinkle JavaScript in the views to handle this. And, as I mentioned I can then refactor for re-use as I see this getting abused.

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