Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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; }
}
like image 932
user2663526 Avatar asked Aug 08 '13 07:08

user2663526


2 Answers

Try this, In Razor view:

@using (Html.BeginForm(new { id = "postform" }))
{
      @Html.HiddenFor(m=>m.hiddevalue)
     <input type="submit" value="Match"/>
}
like image 68
Amit Avatar answered Oct 19 '22 12:10

Amit


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

});
like image 29
Jaimin Avatar answered Oct 19 '22 13:10

Jaimin