asp.net-mvc 如何从 mvc 中的控制器设置隐藏字段的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16870827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the value of a hidden field from a controller in mvc
提问by user2156088
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>
回答by Krunal Solanki
Please find code for respected region.
请查找受尊重地区的代码。
Controller
控制器
ViewBag.hdnFlag= Session["hdnFlag"];
View
看法
<input type="hidden" value="@ViewBag.hdnFlag" id="hdnFlag" />
JavaScript
JavaScript
var hdnFlagVal = $("#hdnFlag").val();
回答by Darin Dimitrov
You could set the corresponding value in the ViewData/ViewBag:
您可以在 中设置相应的值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" })
回答by mokumaxCraig
Without a view model you could use a simple HTML hidden input.
如果没有视图模型,您可以使用简单的 HTML 隐藏输入。
<input type="hidden" name="FullName" id="FullName" value="@ViewBag.FullName" />
回答by sandeep modi
You need to write following code on controller suppose test is model, and Name, Address are field of this model.
您需要在控制器上编写以下代码,假设 test 是模型,而 Name、Address 是该模型的字段。
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.
这将自动设置隐藏值=约翰。
回答by RajeshVerma
Please try using following way.
请尝试使用以下方式。
@Html.Hidden("hdnFlag",(object) Convert.ToInt32(ViewBag.page_Count))
回答by Snziv Gupta
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
否则,如果您使用模型(考虑传递模型具有 hdnFlag 属性),则可以使用此方法
@Html.HiddenFor(model => model.hdnFlag, new { value = Model.hdnFlag})
回答by CyberNinja
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
如果您打算像 id 一样重用该值,或者如果您只想保留它,您可以添加一个 "new{id = 'desiredID/value'}) 作为其参数,以便您可以通过 jquery/javascript 访问该值
@Html.HiddenFor(model => model.Car_id)
回答by Ashwini Gupta
You can transfer value from controller using ViewData[""].
您可以使用ViewData[""].
ViewData["hdnFlag"] = userId;
return View();
Now, In you view.
现在,在你看来。
@{
var localVar = ViewData["hdnFlag"]
}
<input type="hidden" asp-for="@localVar" />
Hope this will help...
希望这会有所帮助...

