从我的SelectListItem中获取值,这样它就可以进入数据库

本文关键字:就可以 数据库 SelectListItem 我的 获取 | 更新日期: 2023-09-27 18:25:51

我很想把我的内容放入数据库,但当我试图把它放入数据库时,它决不允许自己这样做。

在此处写入此错误时没有将值写入数据库的问题。

无法将"System.Collections.Generic.List `1[System.Web.Mvc.SelectListItem]"类型的对象强制转换为"System.IConvertible"类型。

在此处张贴

[HttpPost]
    [AllowAnonymous]
    public ActionResult create(AccountViewModels userindhold)
    {
        DataLinqDB db = new DataLinqDB();
        if (!ModelState.IsValid)
        {
            var username = db.brugeres.FirstOrDefault(i => i.brugernavn == userindhold.Brugernavn);
            if (username == null)
            {
                brugere OpretUser = new brugere();
                OpretUser.kon = Convert.ToInt32(userindhold.Kon);//error her
                OpretUser.birthday = Convert.ToDateTime(userindhold.Birthday);//error her
                OpretUser.nyhedsbrevKategori_Id = Convert.ToInt32(userindhold.KategoriNyhedsbrev);//error her
                db.brugeres.InsertOnSubmit(OpretUser);
                db.SubmitChanges();
                return RedirectToAction("login");
            }
            else
            {
                ModelState.AddModelError("", "Denne e-mail er genkendelig.");
            }
        }
        return View();
    }

获取

[HttpGet]
    [AllowAnonymous]
    public ActionResult create()
    {
        DataLinqDB db = new DataLinqDB();
        AccountViewModels ac = new AccountViewModels();
        ViewBag.Kon = new SelectList(db.Kons, "id", "konvalue");
        ViewBag.KategoriNyhedsbrev = new SelectList(db.KategoriNyhedsbrevs, "id", "tekst");
        return View();
    }

在此建模

[Display(Name = "Hvilken kategori kan du li til nyhedsbrevet?")]
    public IEnumerable<SelectListItem> KategoriNyhedsbrev
    {
        get; set;
    }
    [Display(Name = "Hvilke køn er du?")]
    public IEnumerable<SelectListItem> Kon
    {
        get; set;
    }

我在这里有一个关于这些的表格,还有更多的输入区域

index.cshtml

<div class="form-group">
            <div class="col-xs-12">
                @Html.LabelFor(u => u.Kon)
                @Html.DropDownList("Kon", null, new
           {
               @class = "form-control",
           })
            </div>
        </div>
        <div class="form-group">
            <div class="col-xs-12">
                @Html.LabelFor(u => u.KategoriNyhedsbrev)
                @Html.DropDownList("KategoriNyhedsbrev", null, new
           {
               @class = "form-control",
           })
            </div>
        </div>

从我的SelectListItem中获取值,这样它就可以进入数据库

在您的AccountViewModel中,Kon是类型为IEnumerable<SelectListItem>的属性,您正试图将其传递到Convert.ToInt32方法中。Convert.ToInt32通常需要数字数据的字符串版本(Ex:"34"),以便可以安全地将其转换为整数版本(Ex:34)。此方法无法将列表转换为整数值。

理想情况下,您应该在视图模型中添加一个属性,以保存所选项目值

public class AccountViewModel
{
  public int SelectedKon {set;get;}
  public List<SelectListItem> Kon { get; set; }
  //Your other properties also goes here
}

在你看来,

@model AccountViewModel
@using(Html.BeginForm())
{
  @Html.DropDownListFor(s=>s.SelectedKon, Model.Kon,"Select one item")
  <input type="submit" />
}

现在,在HttpPost操作中,您可以使用已发布模型的SelectedKon属性值

[HttpPost]
public ActionResult Create(AccountViewModel model)
{
    if (ModelState.IsValid)
    {
      brugere OpretUser = new brugere();
      OpretUser.kon = model.SelectedKon;
      //Do other useful things and redirect to a Success page.
    }
    //TO DO : Reload the model.Kon collection again here.
    return View(model)
}

你应该对所有的下降都这样做。

相关文章: