How to use hidden field values from view to controller in asp.net mvc 3

前端 未结 3 1543
滥情空心
滥情空心 2021-02-08 03:38

I have to pass hidden filed values to controller action. So I have tried in the following way, but I am getting null values.

I have tried both methods i.e formcollection

3条回答
  •  醉话见心
    2021-02-08 03:59

    It seems to me like you are trying to get multiple values into the POST controller. In that case, and by your exam, the value from the hidden input is enough. In that case, you can setup your controller as so:

    public ActionResult Index()
    {
        Hidden hd = new Hidden();
        return View(hd);
    }
    
    [HttpPost]
    public ActionResult Index(IEnumerable hiddens)
    {
        foreach (var item in hiddens)
        {
            //do whatter with item
        }
        return View(new Hidden());
    }
    

    and as for your view, simple change it in order to bind to the same name "hiddens" as so:

    @using (Html.BeginForm(new { id = "postform" }))
    {
        
        
    
        
    }
    

    Hope this serves what you are looking forward to.

提交回复
热议问题