2017-11-03 13 views
0

私はasp.net mvcで始まっています。私は私の見解でラジオボタンを持っています。そして私は選択したラジオブトンをデータベースに保存したい。asp.net mvcのデータベースに選択したラジオボタンを保存5

送信ボタンをクリックすると、選択したラジオボタンが送信ボタンの下に表示されます。しかし私は私のデータベースに保存することはできません。選択したラジオボタンをデータベースに保存するにはどうすればいいですか?

ベローは私のコードです。

モデル:

namespace Zillafy.Models 
{ 
    public class ApplicationUser : IdentityUser 
    {   
    public string ExitIntentTemplate { get; set; } 
    } 
} 

コントローラー:

public class DashboardController : Controller 
{ 
    ApplicationDbContext db = new ApplicationDbContext(); 
    public ActionResult ExitIntentDesign(ApplicationUser model) 
    { 
     return View(model); 
    } 
} 

ビュー:

@model Zillafy.Models.ApplicationUser 
@{ 
    ViewBag.Title = "Select Template"; 
} 

@using (Html.BeginForm()) 
{ 
    <div class="col-md-3"> @Html.RadioButtonFor(g => g.ExitIntentTemplate, "Template 1")@Html.Label("Template 1")</div> 
    <div class="col-md-9"> 
     <img width="100%" src="~/Images/template1.jpg"> 
    </div> 

    <div class="col-md-3"> @Html.RadioButtonFor(g => g.ExitIntentTemplate, "Template 2")@Html.Label("Template 2")</div> 
    <div class="col-md-9"> 
     <img width="100%" src="~/Images/template2.jpg"> 
    </div> 

    <input type="submit" value="Submit" /><br /> 
    if (!string.IsNullOrEmpty(Model.ExitIntentTemplate)) 
    { 
    @Html.Label("Your selected option: " + Model.ExitIntentTemplate); 
    } 
} 
+0

POSTメソッドを同じ名前( 'ExitIntentDesign')で定義していますか? 'RadioButtonFor'を使用している場合、' HttpPostAttribute'とマークされた 'ExitIntentDesign(ApplicationUser model)'としてアクションメソッドを作成し、 'Html.BeginForm(" ExitIntentDesign "、" Dashboard "、FormMethod.Post)'をビューに設定してください。 –

答えて

0

コントローラ:

public class DashboardController : Controller 
    { 
     ApplicationDbContext db = new ApplicationDbContext(); 
     public ActionResult ExitIntentDesign() 
     { 
      ApplicationUser model = new ApplicationUser(); 
      return View(model); 
     } 
     [HttpPost] 
     public ActionResult ExitIntentDesign(ApplicationUser model) 
     { 
      // in "model.ExitIntentTemplate" your going to have selected 
      // radiobutton value 
      return View(model); 
     } 
    } 
関連する問題