ASP.NET MVC3剃刀:在控制器检索隐藏字段属性值(使用视图模型) [英] ASP.NET MVC3 RAZOR: Retrieving Hidden field Attribute value in Controller (using ViewModel)
问题描述
我有了两个按钮(第一,第二)和一个隐藏字段的视图。当用户点击第一按钮,隐藏字段将被设置为在第一键数据myAttribute属性的值。当用户点击第二按钮,隐藏字段将被设置为在第二键数据myAttribute属性的值。一旦隐藏字段设置,形式需提交。这多少是工作的罚款与下列code。此外,我需要能够看到在控制器隐藏字段的值。我们如何在控制器获得的价值?
命名空间MyHiddenFieldTest.Controllers
{
公共类ElementController:控制器
{ 公共类MyViewModel
{
公共字符串控件名称{搞定;组; }
} // GET:
公众的ActionResult MyElement()
{
MyViewModel myViewModel =新MyViewModel();
返回查看(myViewModel);
}
// POST:
[HttpPost]
公众的ActionResult MyElement(MyViewModel theViewModel)
{
字符串selectedControl = theViewModel.ControlName;
返回查看(theViewModel); }
}
}
查看
@model MyHiddenFieldTest.Controllers.ElementController.MyViewModel@ {
ViewBag.Title =MyElement;
}< H2> MyElement< / H><脚本SRC =http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js类型=文/ JavaScript的> < / SCRIPT>
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数()
{ $('#mainDiv输入[类型=按钮]')。在('点击',()的函数
{
$('#从')VAL($(本).attr('数据myAttribute')); 警报($('#从')VAL());
$(本).closest(形式)提交();
});});
< / SCRIPT>
@using(Html.BeginForm())
{< DIV ID =mainDiv>
<输入类型=按钮值=第一数据myAttribute =最早出现的/>
<输入类型=按钮值=秒数据myAttribute =theSecond/>
<从值=1/&GT输入类型=隐藏ID =;
<输入类型=隐藏ID =到价值=2/>
< / DIV>}
阅读
ASP.Net MVC自动绑定操作参数,形成岗位。结果
刚刚从参数字符串添加到操作方法。
不过,您还需要将 NAME =
为浏览器,以便投入到他们提交
然而,实际上,你的整个脚本是不必要的。
只要写
<输入类型=提交名称=从价值=第一/>
<输入类型=从价值=秒/&GT提交名称=;
浏览器将提交名称/值对被点击任何按钮。
I have a view that has two buttons("First" and "Second") and one hidden field. When user clicks on "First" button, hidden field will be set as the value of "data-myAttribute" attribute in the first button. When user clicks on "Second" button, hidden field will be set as the value of "data-myAttribute" attribute in the second button. Once the hidden field is set, the form need to be submitted. This much is working fine with the following code. Further, I need to be able to see the value of the hidden field in the controller. How do we get the value in controller?
namespace MyHiddenFieldTest.Controllers
{
public class ElementController : Controller
{
public class MyViewModel
{
public string ControlName { get; set; }
}
// GET:
public ActionResult MyElement()
{
MyViewModel myViewModel = new MyViewModel();
return View(myViewModel);
}
// POST:
[HttpPost]
public ActionResult MyElement(MyViewModel theViewModel)
{
string selectedControl = theViewModel.ControlName;
return View(theViewModel);
}
}
}
VIEW
@model MyHiddenFieldTest.Controllers.ElementController.MyViewModel
@{
ViewBag.Title = "MyElement";
}
<h2>MyElement</h2>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"> </script>
<script type="text/javascript">
$(document).ready(function ()
{
$('#mainDiv input[type="button"]').on('click', function ()
{
$('#from').val($(this).attr('data-myAttribute'));
alert($('#from').val());
$(this).closest('form').submit();
});
});
</script>
@using (Html.BeginForm())
{
<div id="mainDiv">
<input type="button" value="First" data-myAttribute="theFirst" />
<input type="button" value="Second" data-myAttribute="theSecond" />
<input type="hidden" id="from" value="1" />
<input type="hidden" id="to" value="2" />
</div>
}
READING
ASP.Net MVC automatically binds action parameters to form posts.
Just add a string from
parameter to the action method.
However, you will also need to name=""
the inputs in order for the browser to submit them.
Actually, however, your entire script is unnecessary.
Just write
<input type="submit" name="from" value="First" />
<input type="submit" name="from" value="Second" />
The browser will submit the name / value pair of whichever button was clicked.
这篇关于ASP.NET MVC3剃刀:在控制器检索隐藏字段属性值(使用视图模型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!