简体   繁体   English

选择HTML.RadioButtonFor后,如何为model属性赋值

[英]How to assign a value to model property once HTML.RadioButtonFor is selected

I am new to MVC . 我是MVC新手。 Currently I have a strongly typed view, and I use two Html.RadioButtonFor for labels, the codes are somethings like: 目前我有一个强类型视图,我使用两个Html.RadioButtonFor作为标签,代码是像这样的东西:

<label class="radio">
       @Html.RadioButtonFor(model => model.Incoming, true)IncomingItems</label>
 <label class="radio">
       @Html.RadioButtonFor(model => model.Incoming, false)OutgoingItems</label>
 <input type="hidden" id="isIncomingItem" value = "@Model.Incoming" />

What I want is to set model.Incoming to be true when the first radio button is selected or set it to false, and then I want to use this model property right away in the view( assign this value to "isIncomingItem" ) before the entire form is submitted to server. 我想要的是设置model.Incoming在选择第一个单选按钮时为true或将其设置为false,然后我想在视图中立即使用此模型属性(将此值赋给"isIncomingItem" )之前整个表单提交给服务器。

Do you guys have a idea about how I could achieve it. 你们对我如何实现它有所了解吗? Really appreciated! 非常感谢!

Your Incoming property is already filled with the selected value and it will be available for the action method. 您的Incoming属性已填充所选值,并且可用于操作方法。 And if you want to set the same value in the hidden field, try as following 如果要在隐藏字段中设置相同的值,请尝试以下操作

@Html.HiddenFor(m => m.Incoming,new { id = "isIncomingItem"})

You'd have to use jQuery, doing something like: 你必须使用jQuery,做类似的事情:

$("[name='Incoming']").change(function () {
    $("#isIncomingItem").val($(this).val());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM