【问题标题】:How to pass radio button for selected value to the controller?如何将选定值的单选按钮传递给控制器​​?
【发布时间】:2017-11-30 02:13:23
【问题描述】:

我在 MVC 中有一个强类型单选按钮。如何将值保存到模型属性

<ul class="formlist">
    <li class="width100">
        <div class="form-check form-check-inline">
            <label class="form-check-label">
                @Html.RadioButtonFor(model => model._Account[i].account_flag, 1, new { @id = Model._Service[i].service_id, @Name = "text" + @Model._Service[i].service_id, @checked = "false" })<span>Yes</span>
            </label>
        </div>
        <div class="form-check form-check-inline">
            <label class="form-check-label">
                @Html.RadioButtonFor(model => model._Account[i].account_flag, 0, new { @id = Model._Service[i].service_id, @Name = "text" + @Model._Service[i].service_id, @checked = "checked" })<span>No</span>
            </label>
        </div>
    </li>
</ul>

【问题讨论】:

    标签: asp.net-mvc radio-button strong-typing


    【解决方案1】:

    这段代码对我来说很好用。希望这会很有用。

    查看:

    @using (Html.BeginForm("Test", "Home", FormMethod.Post, new { @class = "my_form" }))
    {
    <ul class="formlist">
      <li class="width100">
        <div class="form-check form-check-inline">
          <label class="form-check-label">
            @Html.RadioButtonFor(model => model._Account[i].account_flag, 1, new { id = Model._Service[i].service_id, Name = "text" + @Model._Service[i].service_id, @checked = "false" })<span>Yes</span>
          </label>
        </div>
        <div class="form-check form-check-inline">
          <label class="form-check-label">
            @Html.RadioButtonFor(model => model._Account[i].account_flag, 0, new { id = Model._Service[i].service_id, Name = "text" + @Model._Service[i].service_id, @checked = "checked" })<span>No</span>
          </label>
        </div>
    </li>
    

    }

    型号:

    using System.Collections.Generic;
    
      namespace sample_project.Models
      {
        public class SampleModel
        {
            public List<Account> _Account { get; set; }
        }
    
        public class Account
        {
            public string account_flag { get; set; }
            public int service_id { get; set; }
        }
      }
    

    控制器:

    using sample_project.Models;
    using System.Collections.Generic;
    using System.Web.Mvc;
    
    namespace sample_project.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                SampleModel model = new SampleModel();
                model._Account = new List<Account>();
                model._Account.Add(new Account() { service_id=1 });
                return View(model);
            }
    
            [HttpPost]
            public ActionResult Test(SampleModel model)
            {
                string account_flag = model._Account[0].account_flag;
    
                return View();
            }
        }
    

    【讨论】:

    • 发布到控制器时,account_flag 属性仍然为空
    • 能否分享一下控制器和模型代码的get和post方法
    猜你喜欢
    • 2019-07-06
    • 1970-01-01
    • 2019-03-24
    • 2014-03-26
    • 1970-01-01
    • 2021-08-16
    • 2014-04-08
    • 2022-06-26
    • 2018-01-09
    相关资源
    最近更新 更多