如何传递给下拉编辑模式字段MVC4的价值? [英] How to pass the value to drop down fields in edit mode in MVC4?

查看:221
本文介绍了如何传递给下拉编辑模式字段MVC4的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有我的view.That三个字段的三个区域是降下来了。我想点击编辑按钮时的值传递给这些字段。这是值需要传递给该下拉字段。我的看法如下

Hi i have three Fields in my view.That three fields are drop down. I want to pass the value to these fields when edit button is clicked. That is the values need to pass to that drop down fields. My view is mentioned below

下拉

在我看来,我有很多的下拉菜单,但一旦我知道如何将值传递给一个下拉意味着我将另一个下拉菜单做的。

In my view i have many drop downs but once i know how to pass the value to one drop down means i will do for another drop downs.

有关编辑我创建SQL中的一个视图和这个视图(表)我有所有领域这是参观者形式连接的视图EDMX文件在我application.In。这种观点的名字是View_VisitorsForm。

For Edit i create one view in sql and connect that view as EDMX file in my application.In this view(table) i have all fields which is in Visitors Form. That view name is View_VisitorsForm.

我的模型(VisitorsViewModel)

    public Nullable<System.DateTime> Date { get; set; }
    public System.Guid VisitingID { get; set; }
    public Nullable<System.Guid> EmployeeID { get; set; }
    public string EmployeeName { get; set; }
    public string Description { get; set; }

我的编辑code

      public ActionResult Edit(Guid ?id)
      {
             WafeERPNEWEntities db = new WafeERPNEWEntities();
        SelectList typelist = new SelectList(db.Employees.ToList(), "EmployeeID", "DisplayName",  db.Employees);
        ViewData["EmployeeName"] = typelist;
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        VisitorsViewModel ObjVisitorsviewModel = new VisitorsViewModel();
        View_VisitorsForm visit = db.View_VisitorsForm.Find(id);
        visit.VisitingID = ObjVisitorsviewModel.VisitingID;
        visit.VisitingDate = ObjVisitorsviewModel.Date;
        visit.Description = ObjVisitorsviewModel.Description;
                 if (ObjVisitorsviewModel == null)
        {
            return HttpNotFound();
        }

        return View(ObjVisitorsviewModel);
    }

我查看code

        @Html.LabelFor(model => model.Date)
        @Html.EditorFor(model => model.Date)
        @Html.ValidationMessageFor(model => model.Date)

        @Html.LabelFor(model => model.VisitingID)
        @Html.EditorFor(model => model.VisitingID)
        @Html.ValidationMessageFor(model => model.VisitingID)

        @Html.LabelFor(model => model.EmployeeName)
        @Html.DropDownList("EmployeeID", (IEnumerable<SelectListItem>)ViewData["EmployeeName"])
        @Html.ValidationMessageFor(model => model.EmployeeName)

现在,当我点击编辑按钮,它的价值传递给该行

Now when i click the edit button it pass the value to this line

    View_VisitorsForm visit = db.View_VisitorsForm.Find(id);

并且也越来越visit.visitingID。但它不是在视图模型。所以价值所获得的价值将是空的view.All值为空VisitingID,说明,日期字段为空,并在员工下拉它不会显示我传递给此字段的值是显示下拉列表的第一个值。所以请任何一个可以告诉我怎么解决这个问题。其实我试图解释我的问题按我的级别最好的,如果你不明白我的问题,或者任何一个需要我的全code或需要更多code告诉我。我准备再次更新我的code。但我需要的解决方案。

and also it getting visit.visitingID. But it is not getting the value in viewmodel .so the value will be empty in view.All values are empty VisitingID, Description, Date Fields are empty and in Employee drop down it won't show the value which i passed to this field it shows the first value in dropdown. so please any one tell me how to solve this issue. Actually I try to explain my issue as per my level best and if you didn't understand my issue or any one need my full code or need more code tell me . i ready to update my code again. but i need solution.

先谢谢..

推荐答案

您正在使用的的DropDownList(...)代替 DropDownListFor (...)

您的型号

您必须添加的SelectList:

You must add a SelectList:

public SelectList Employees { get; set }

您的修改

您必须让你的员工名单,并将其添加到您的模型:

You must get your employees list and add it to your model:

// Get employees list from the database
var employees = db.Employee.Select(x => x.Id, x.Name).Tolist();

// Put the employees in a SelectList
var selectList = new SelectList(employees .Select(x => new { value = x.Id, text = x.Name }), "value", "text");}).ToList();

// Pass the list to your ViewModel
ObjVisitorsviewModel.Employees = selectList;

您的视图

最后,改变你的DropDownListFor线这样的:

Finally, change your DropDownListFor line for this:

@Html.DropDownListFor(model => model.EmployeeID,
                               model.Employees)

通过使用的DropDownList(...),你的对象数据未绑定到下拉。您必须手动管理其选定的值。

By using DropDownList(...), your object data is not bound to the DropDown. You must manage its selected value manually.

这篇关于如何传递给下拉编辑模式字段MVC4的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆