【问题标题】:save selected radiobutton to database in asp.net mvc 5在asp.net mvc 5中将选定的单选按钮保存到数据库
【发布时间】:2018-04-15 17:31:28
【问题描述】:

我是 asp.net mvc 的初学者。我的视图中有单选按钮。我想将选定的单选按钮保存到我的数据库中。

当我点击提交按钮时,选中的单选按钮显示在提交按钮下。但我无法保存到我的数据库。如何将选中的单选按钮保存到我的数据库中?

下面是我的代码。

型号:

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

【问题讨论】:

  • 你是否定义了同名的POST方法(ExitIntentDesign)?如果您使用的是RadioButtonFor,只需创建操作方法为ExitIntentDesign(ApplicationUser model),并标记HttpPostAttribute,并设置Html.BeginForm("ExitIntentDesign", "Dashboard", FormMethod.Post) 以供查看。

标签: asp.net-mvc asp.net-mvc-5 radio-button


【解决方案1】:

控制器:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 2020-08-02
    相关资源
    最近更新 更多