插入razor mvc下拉列表中的选定值

插入razor mvc下拉列表中的选定值,razor,asp.net-mvc-4,Razor,Asp.net Mvc 4,您好,我有一个项目,我的任务之一是从中插入选定的值 通过razor mvc下拉到DB字段。我编写了代码,但没有插入值,DDL也有来自DB的项。我与razor mvc4的项目 public ActionResult Create() { var data=db.Categories.ToList().Distinct(); foreach(数据中的var t) { s、 Text=t.名称; s、 Value=t.Cat_ID.ToString(); 项目。添加(s); } ViewBag.Pa

您好,我有一个项目,我的任务之一是从中插入选定的值 通过razor mvc下拉到DB字段。我编写了代码,但没有插入值,DDL也有来自DB的项。我与razor mvc4的项目

public ActionResult Create()
{
var data=db.Categories.ToList().Distinct();
foreach(数据中的var t)
{
s、 Text=t.名称;
s、 Value=t.Cat_ID.ToString();
项目。添加(s);
}
ViewBag.Parent=项目;
返回视图();
}
[HttpPost]
公共操作结果创建(类别,IEnumerable文件)
{
如果(Request.Files.Count>0)
{
var uploadedFile=Request.Files[0];
var fileSavePath=“”;
var fileName=“”;
fileName=Path.GetFileName(uploadedFile.fileName);
fileSavePath=Server.MapPath(“~/App\u Data/Uploads/”+文件名);
uploadedFile.SaveAs(fileSavePath);
category.Path=“~/App\u Data/Uploads/”+文件名;
}
var data=db.Categories.ToList().Distinct();
列表项=新列表();
foreach(数据中的var t)
{
SelectListItems=新建SelectListItem();
s、 Text=t.名称;
s、 Value=t.Cat_ID.ToString();
项目。添加(s);
如果(已选定)
{category.Parent_ID=int.Parse(s.Value);}
}
db.Categories.Add(类别);
db.SaveChanges();
返回操作(“索引”);
}

您正在同一循环中创建新的SelectListItems。对于任何项,您都将得到seleced==true

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

我怀疑这是一个逻辑错误


您好,Mahesh你好,您的代码应该是这样的。我假设你们有一个具有一定结构的模型。如果贝洛代码没有给你一个线索,那么请。告诉你的视图和模型是怎样的,你的需求是什么

希望这有帮助

[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");
}
[HttpPost]
公共操作结果创建(类别,IEnumerable文件)
{
如果(Request.Files.Count>0)
{
var uploadedFile=Request.Files[0];
var fileSavePath=“”;
var fileName=“”;
fileName=Path.GetFileName(uploadedFile.fileName);
fileSavePath=Server.MapPath(“~/App\u Data/Uploads/”+文件名);
uploadedFile.SaveAs(fileSavePath);
category.Path=“~/App\u Data/Uploads/”+文件名;
}
var data=db.Categories.ToList().Distinct();
//我假设您需要从db.Caegories中查找用户在UI上选择并提交表单的类别。
//我假设参数类别是您希望从DB中找到的类别。
//类别是指您的模型具有绑定到UI控件(即下拉列表)的父对象Id属性
var categoryToSave=(数据中的c)
其中c.Cat\u ID==category.Parent\u ID
选择c).firstOrDefault();
如果(categoryToSave!=null)
{
//我相信这里您希望将此类别保存到其他表中。
//现在您已经获得了用户在UI上选择的类别。
//在这里写下进一步的逻辑。。。。。
db.SaveChanges();
}
返回操作(“索引”);
}

您好,Mahesh

告诉我们您的需求以及您的模型是如何构造的。我认为您需要使用模型。将该模型绑定到视图。当您提交该视图时,您将在模型中获得用户选择的值。
[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");
}