当我们使用java Script自动选中复选框时,如何更新模型值? [英] How to update the model value when we auto select the check box using the java Script?
本文介绍了当我们使用java Script自动选中复选框时,如何更新模型值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
@ Html.CheckBoxFor(model => model.IsPrint,new {id =sendCorrMail})@ Html.LabelFor(m => m.IsPrint)
这是我检查复选框的方式,但是当我自动选择时,model.Isprint没有被传递为true?
$('#sendCorrMail')。attr('checked','checked');
解决方案
('#sendCorrMail')。attr('checked ','选中');
尝试如下
将下面的标记放在下面
@model SampleModel
@using(Html.BeginForm(PostCheckBox,Sample,FormMethod.Post))
{
@ Html.CheckBoxFor(model = > model.IsChecked,new {id =sendCorrMail})
< input type =submitvalue =Click/>
}
创建模型类
namespace MVCFileUpload.Models
{
public class SampleModel
{
public bool IsChecked {获得跨度>; set ; }
}
}
创建一个ActionResult[HttpPost]
public ActionResult PostCheckBox(SampleModel sampleModel)
{
return 查看( 索引);
}
希望这有帮助
我是用以解决问题Jquery的帮助...这就是我为解决这个问题所做的工作..
这是我添加的隐藏字段.. 。
@ Html.HiddenFor(model => model.IsMailMandatory,new {@class =hiddenIsMailMandatory})
这是jquery ...
// 如果邮件是唯一的交付选项,则自动选择邮件。
如果 (
@Html.CheckBoxFor(model => model.IsPrint, new { id = "sendCorrMail" }) @Html.LabelFor(m => m.IsPrint)
this is how I am checking the check box but the model.Isprint is not being passed as true when I auto select?
$('#sendCorrMail').attr('checked', 'checked');
解决方案
('#sendCorrMail').attr('checked', 'checked');
Try like below
Put the below markup like below
@model SampleModel @using (Html.BeginForm("PostCheckBox", "Sample", FormMethod.Post)) { @Html.CheckBoxFor(model => model.IsChecked, new { id = "sendCorrMail" }) <input type="submit" value="Click"/> }
Create a Model class
namespace MVCFileUpload.Models { public class SampleModel { public bool IsChecked { get; set; } } }
Create an ActionResult[HttpPost] public ActionResult PostCheckBox(SampleModel sampleModel) { return View("Index"); }
Hope this helps
I was to resolve the issue with the help of Jquery... this is what I did to resolve the issue..
this is the hidden field I added...
@Html.HiddenFor(model => model.IsMailMandatory, new { @class = "hiddenIsMailMandatory" })
this is the jquery...
//to auto select the Mail if the mail is the only delivery option. if (
这篇关于当我们使用java Script自动选中复选框时,如何更新模型值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文