11

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 and viewmodel concept

Controller

public ActionResult MapIcon()
{
    Hidden hd = new Hidden();
    return View(hd);
}

[HttpPost]
public ActionResult MapIcon(Hidden hidden)
{
    var value=hidden.hiddevalue;//null
    FormCollection col = new FormCollection();
    var value = col["hidden1"];
  //  string value = mycontroler.ControlName;

    return View(hidden);
}

View

@model SVGImageUpload.Models.Hidden
Razor view:@using (Html.BeginForm(new { id = "postform" }))
{
    <input type="hidden" id="" value="7" name="hidden1" />
    <input type="hidden" id="" value="7"  name="hidden2"/>

    <input type="submit" value="Match"/>
}

Viewmodel

public class Hidden
{
  public string hiddevalue { get; set; }
}
1
  • @mattytommo i am not taking values from database so didn't have model class .I have only view model.My main concerns is how to post values of input field i.e 7 in my eg.Is it possible ? Aug 8, 2013 at 7:59

3 Answers 3

10

Try this, In Razor view:

@using (Html.BeginForm(new { id = "postform" }))
{
      @Html.HiddenFor(m=>m.hiddevalue)
     <input type="submit" value="Match"/>
}
2
  • 1
    how i wil get the values of hiddenvalue .AS i am not taking these values from database. Aug 8, 2013 at 8:06
  • when you submit the form, the MVC ModelBinders will automatically pick up the value from "@Html.HiddenFor" and send it to the action method. So it will cast the value in "@Html.HiddenFor" to Hidden object type. This happens because you are strongly typing the view the viewmodel. Aug 8, 2013 at 10:45
9

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<string> 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" }))
{
    <input type="hidden" value="7" name="hiddens" />
    <input type="hidden" value="2" name="hiddens" />

    <input type="submit" value="Match" />
}

Hope this serves what you are looking forward to.

3

if your hidden value is static.Than try this

View

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


 @Html.HiddenFor(m=>m.hiddevalue)
    <input type="hidden" id="" value="7" name="hidden1" />
    <input type="hidden" id="" value="7"  name="hidden2"/>

    <input type="submit" value="Match"/>
}

Controller

[HttpPost]
public ActionResult MapIcon(Hidden hidden, string hidden1, string hidden2)
{
    var hiddenvalue = hidden.hiddevalue;
    var hiddenvalue1 = hidden1;
    var hiddenvalue2 = hidden2;
    var value=hidden.hiddevalue;//null
    FormCollection col = new FormCollection();
    var value = col["hidden1"];
  //  string value = mycontroler.ControlName;

    return View(hidden);
}

Script

 $(document).ready(function () {

 $('#hiddevalue').val("Jaimin");

});
5
  • 1
    your idea is fine but my problem is a little bit different Aug 8, 2013 at 9:42
  • your idea is fine but my problem is a little bit different let me explain the problem is as follows How can i show those images that are appended to form dynamically. i.e i can append hidden input filed those have attributes image id,value etc to form using jquery append methods. Aug 8, 2013 at 9:49
  • you don't explain that in your question.
    – Jaimin
    Aug 8, 2013 at 9:51
  • Ya i didn't explain the problem .Do you have some idea .how do that? Aug 8, 2013 at 9:55
  • HI i just update my code now you get append value in controller
    – Jaimin
    Aug 8, 2013 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.