14

I want to set the value of a hidden field from a controller.How can i do this?

In view part i have given like this..

 <div>
@Html.Hidden("hdnFlag", null, new { @id = "hdnFlag" }) 
</div>
user2156088
  • 2,350
  • 5
  • 20
  • 24
  • @Html.HiddenFor(x => x.UserName); – Vishal Sharma Jan 15 '14 at 18:15
  • if you are not using model as per your question you can do like this @Html.HiddenFor("hdnFlag" , new {id = "hdnFlag", value = "hdnFlag_value" }) if you are using model @Html.HiddenFor(model => model.hdnFlag, new { value = Model.hdnFlag}) – Snziv Gupta Apr 26 '18 at 22:10

8 Answers8

19

You could set the corresponding value in the ViewData/ViewBag:

ViewData["hdnFlag"] = "some value";

But a much better approach is to of course use a view model:

model.hdnFlag = "some value";
return View(model);

and use a strongly typed helper in your view:

@Html.HiddenFor(x => x.hdnFlag, new { id = "hdnFlag" })
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • If the hidden field is not updated, put `ModelState.Remove("hdnFlag")` into your controller. See https://stackoverflow.com/a/9402617/373126 – Jan Šotola Jun 01 '22 at 20:03
15

Please find code for respected region.

Controller

ViewBag.hdnFlag= Session["hdnFlag"];

View

<input type="hidden" value="@ViewBag.hdnFlag" id="hdnFlag" />

JavaScript

var hdnFlagVal = $("#hdnFlag").val();
Krunal Solanki
  • 511
  • 5
  • 10
  • 1
    Using the user session to store a value may make the application more complex. In the very least you now have to worry whether the value in the session is valid whenever you need to use it. – Geeky Guy Dec 12 '16 at 13:57
7

Without a view model you could use a simple HTML hidden input.

<input type="hidden" name="FullName" id="FullName" value="@ViewBag.FullName" />
mokumaxCraig
  • 413
  • 1
  • 7
  • 15
6

You need to write following code on controller suppose test is model, and Name, Address are field of this model.

public ActionResult MyMethod()
{
    Test test=new Test();
    var test.Name="John";
    return View(test);   
}

now use like like this on your view to give set value of hidden variable.

@model YourApplicationName.Model.Test

@Html.HiddenFor(m=>m.Name,new{id="hdnFlag"})

This will automatically set hidden value=john.

Ant P
  • 24,820
  • 5
  • 68
  • 105
sandeep modi
  • 106
  • 4
4

Please try using following way.

@Html.Hidden("hdnFlag",(object) Convert.ToInt32(ViewBag.page_Count))
Ivin Raj
  • 3,448
  • 2
  • 28
  • 65
RajeshVerma
  • 1,087
  • 9
  • 13
4

if you are not using model as per your question you can do like this

@Html.Hidden("hdnFlag" , new {id = "hdnFlag", value = "hdnFlag_value" })

else if you are using model (considering passing model has hdnFlag property), you can use this approch

@Html.HiddenFor(model => model.hdnFlag, new { value = Model.hdnFlag})
Snziv Gupta
  • 1,016
  • 1
  • 10
  • 21
0

If you're going to reuse the value like an id or if you want to just keep it you can add a "new{id = 'desiredID/value'}) as its parameters so you can access the value thru jquery/javascript

@Html.HiddenFor(model => model.Car_id)
CyberNinja
  • 872
  • 9
  • 25
0

You can transfer value from controller using ViewData[""].

ViewData["hdnFlag"] = userId;
return View();

Now, In you view.

@{
    var localVar = ViewData["hdnFlag"]
}
<input type="hidden" asp-for="@localVar" />

Hope this will help...