2017-11-03 5 views
0

Ich bin beginer mit asp.net mvc. Ich habe Radiobutton aus meiner Sicht. Und ich möchte das ausgewählte Radiobuton in meiner Datenbank speichern.speichern ausgewählten Radiobutton zu Datenbank in asp.net mvc 5

Wenn ich auf "Senden" -Button klicke, wird der ausgewählte Radiobutton unter "Senden" angezeigt. Aber ich kann nicht in meiner Datenbank speichern. Wie speichere ich den ausgewählten Radiobutton in meiner Datenbank?

Unten sind meine Codes.

Modell:

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

Controller:

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

Ausblick:

@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

Sind Sie POST-Methode mit demselben Namen definiert ('ExitIntentDesign')? Wenn Sie 'RadioButtonFor' verwenden, erstellen Sie einfach eine Aktionsmethode als' ExitIntentDesign (ApplicationUser-Modell) ', die mit' HttpPostAttribute' markiert ist und 'Html.BeginForm (" ExitIntentDesign "," Dashboard ", FormMethod.Post)' für die Ansicht. –

Antwort

0

Controller:

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); 
     } 
    }