1

嗨,我有一个项目,我的任务之一是通过 razor mvc 从下拉列表中插入选定的值到 DB 字段。我做了我的代码,但没有插入任何值,DDL 也有来自 DB 的项目。我的剃须刀 mvc4 项目。

 public ActionResult Create()
    {
        var data = db.Categories.ToList().Distinct();

        foreach (var t in data)
        {

            s.Text = t.Name;
            s.Value = t.Cat_ID.ToString();
            items.Add(s);
        }
        ViewBag.Parent = items;

        return View();
    }


[HttpPost]
    public ActionResult Create(Category category, IEnumerable<HttpPostedFileBase> files)
    {

        if (Request.Files.Count > 0)
        {
            var uploadedFile = Request.Files[0];

            var fileSavePath = "";
            var fileName = "";


            fileName = Path.GetFileName(uploadedFile.FileName);
            fileSavePath = Server.MapPath("~/App_Data/Uploads/" + fileName);

            uploadedFile.SaveAs(fileSavePath);
            category.Path = "~/App_Data/Uploads/" + fileName;
        }

        var data = db.Categories.ToList().Distinct();
        List<SelectListItem> items = new List<SelectListItem>();
        foreach (var t in data)
        {
            SelectListItem s = new SelectListItem();
            s.Text = t.Name;
            s.Value = t.Cat_ID.ToString();
            items.Add(s);
            if (s.Selected)
            { category.Parent_ID = int.Parse(s.Value); }
        }

        db.Categories.Add(category);
        db.SaveChanges();

        return RedirectToAction("Index");
    }
4

2 回答 2

0

您正在同一个循环中创建新的 selectListItem 。对于任何项目,您都将被选中 == true。

如果您的模型绑定到所需的下拉列表,则可能是用户在 UI 上(发布之前)选择的项目,存在于类别参数中。

我怀疑这是一个逻辑错误。

问候, 马赫什

于 2013-01-08T23:45:14.123 回答
0

嗨,您的代码应该是这样的。我假设您有具有特定结构的模型。如果贝洛代码没有给你一个线索,那么请。说明您的视图和模型如何以及您的要求是什么。

希望这可以帮助。

[HttpPost]
public ActionResult Create(Category category, IEnumerable<HttpPostedFileBase> files)
{

    if (Request.Files.Count > 0)
    {
        var uploadedFile = Request.Files[0];

        var fileSavePath = "";
        var fileName = "";


        fileName = Path.GetFileName(uploadedFile.FileName);
        fileSavePath = Server.MapPath("~/App_Data/Uploads/" + fileName);

        uploadedFile.SaveAs(fileSavePath);
        category.Path = "~/App_Data/Uploads/" + fileName;
    }

    var data = db.Categories.ToList().Distinct();

    //I assume that you need to find the category from db.Caegories which user has selected on the UI and submited the form.
    //I assume the parameter category is the one which you want to find from DB.
    //category is your model has Parent_Id property which is bound to UI control (ie. dropdown) 
    var categoryToSave = (from c in data
                            where c.Cat_ID == category.Parent_ID
                            select c).firstOrDefault();
    if(categoryToSave!=null)
    {
//I believe here you want to save this category to some other table. 
//Now you have got the category tht user has selected on UI. 
//Write the further logic here.....

        db.SaveChanges();
    }
    return RedirectToAction("Index");
}

问候, 马赫什

于 2013-01-12T20:34:18.547 回答