2017-06-27 26 views
-1

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> 

答えて

0

このコードは私にとってうまくいきます。これが役に立つと願っています。

ビュー:

@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(); 
     } 
    } 
+0

コントローラへの投稿、account_flagプロパティがまだnullです –

+0

コントローラとモデルコードのgetメソッドとpostメソッドを共有してください – John

関連する問題