繁体   English   中英

选定的 Radiobutton 值未发布到 controller

[英]Selected Radiobutton value is not getting posted to controller

我试图在我的永久地址和邮寄地址视图中有 2 个单选按钮。 但是所选单选按钮的值没有回传到 controller 中的 Save 方法。当它到达控制器的 Save 方法时它是 null。我将属性设置为“隐藏”,认为它会被发布,但它不是.

我尝试了两种不同的方式来显示单选按钮——a) 通过对两个单选按钮使用一个属性“isPermanentAddress”;b) 通过使用两个不同的属性“PermanentAddress”和“MailingAddress”。 但是两者都没有用。

我究竟做错了什么? 提前致谢!

我的代码:

查看:

<div>
   <span>@Html.RadioButtonFor(model => model.IsPermanentAddress, true) Permanent Address       
   </span>
    <br />
   <span> @Html.RadioButtonFor(model => model.IsPermanentAddress, false) Mailing Address  
   </span>

或者

<div>
   <span>@Html.RadioButtonFor(model => model.PermanentAddress, false) Permanent </span>
   <br />
   <span> @Html.RadioButtonFor(model => model.MailingAddress, false) Mailing </span>
 </div>

@Html.HiddenFor(model => model. IsPermanentAddress)
@Html.HiddenFor(model => model.PermanentAddress)
@Html.HiddenFor(model => model.MailingAddress)

Model : 地址

public bool? PermanentAddress { get; set; }

public bool? MailingAddress { get; set; }

public bool? IsPermanentAddress { get; set; }

Controller :

[HttpPost]
public ActionResult Save(Address address, bool? isPermanentAddress, bool? PermanentAddress, bool? MailingAddress)
{
   //code to process the data posted from the view.
}

这是 HTML 在浏览器中的样子。

IsPermanentAddress 的“真”是因为我在视图中是“真”。 当我发送 select 邮件时,它在浏览器中仍然是“False”。

 <input id="IsPermanentAddress " name="IsPermanentAddress " type="radio" value="True"> " Permanent Address " <input id=" isPermanentAddress" name=" isPermanentAddress " type="radio" value="False"> " Mailing Address " OR <input id="PermanentAddress " name="PermanentAddress " type="radio" value="False"> " Permanent " <input id="MailingAddress " name=" MailingAddress " type="radio" value="False"> " Mailing "

您可以尝试将单选按钮的值设置为true。当单选按钮被选中时,值true将传递给action,或者不传递该值。

<div>
   <span>@Html.RadioButtonFor(model => model.IsPermanentAddress, true) Permanent Address       
   </span>
    <br />
   <span> @Html.RadioButtonFor(model => model.IsPermanentAddress, true) Mailing Address  
   </span>
</div>

暂无
暂无

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

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