简体   繁体   English

动态视图MVC4中的下拉菜单

[英]Dropdown in dynamic views mvc4

We are using dynamic views to display and edit the records. 我们正在使用动态视图来显示和编辑记录。

Say I have a model A: 假设我有一个模型A:

public class A
{
    public int AID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int organizationid { get; set; }
}

And class B: 和B类:

public class B
{
    public int organizationid { get; set; }
    public string orgnmae { get; set; }
    public string orgdesc { get; set; }
}

here organizationid of table B is foreign key in table A. 表B的organizationid是表A中的外键。

Now I have dynamic editor view, 现在我有了动态编辑器视图,

@model dynamic
@using (Html.BeginForm("Edit", null,  FormMethod.Post))
 {
   @Html.EditorForModel()

 <input type="submit" value="Edit" />
}

If we choose to edit any A's record, it will display this editor view with all text boxes. 如果我们选择编辑任何A的记录,它将显示带有所有文本框的此编辑器视图。 But for organizationid I should not show integer value in it. 但是对于organizationid,我不应该在其中显示整数值。 Instead I should show a dropdown with available organizations of table B and user can choose one of them to edit. 相反,我应该显示表B的可用组织的下拉列表,用户可以选择其中一个进行编辑。

How can I handle this? 我该如何处理? I read Darin's answers here ; 在这里阅读了达林的答案 that's a good suggestion, but in my case the dropdown items should be part of another model. 这是一个很好的建议,但就我而言,下拉菜单项应该是另一个模型的一部分。

If I was tackling this problem in a way you are suggesting, then I would make a change to A. If you aren't going to use a viewmodel, then you will need to extend it's properties. 如果我按照您的建议解决此问题,则可以对A进行更改。如果您不打算使用viewmodel,则需要扩展它的属性。

public class A
{

    [ScaffoldColumn(false)]
    public int OrganizationId { get; set; }


    [Display(Name="OrganizationId")]
    [UIHint("DropDownList")]
    public IEnumerable<SelectListItem> Organizations { get; set; }
}

You notice that I have overridden the name field of the Organizations so that the value is fed back into model binding as OrganizationId. 您会注意到,我已经覆盖了组织的名称字段,以便将值作为OrganizationId反馈到模型绑定中。 When you build this model, you are going to have create the orgranizations property as a list or array of selectlistitems from the organisation instances that are relevant. 建立此模型时,将要创建orgranizations属性,作为相关组织实例中selectlistitems的列表或数组。

I have also set the original organizationid to not be scaffolded so hopefully that will stop it participating in the render process. 我还将原始的organizationid设置为不被脚手架打住,希望它能阻止它参与渲染过程。

You will of course need to make the template DropDownList in your editortemplates. 您当然需要在编辑器模板中创建模板DropDownList。

HTH HTH

I'm not too sure if I understand your question correctly. 我不太确定我是否正确理解您的问题。 I never use EditorFor , I just add the fields directly from the view model. 我从不使用EditorFor ,我只是直接从视图模型添加字段。 It gives me more control. 它给了我更多的控制权。 From what I can gather it seems like you want to edit class A and have class B in a drop down list? 据我所知,您似乎想编辑class A并在下拉列表中添加class B

This is how I will define class A and class B given the names of your properties, please excuse if I am mistaken. 给定您的属性名称,这就是我将如何定义class Aclass B class A ,如果我记错了,请原谅。 class A is class User and class B is class Organization . class A is class User class B is class Organization You need to give your properties better descriptions so that people can read them better. 您需要对属性进行更好的描述,以便人们可以更好地阅读它们。

public class User
{
     public int UserId { get; set; }

     public string FirstName { get; set; }

     public string LastName { get; set; }

     public int OrganizationId { get; set; }
}

public class Organization
{
     public int OrganizationId { get; set; }

     public string OrganizationName { get; set; }

     public string OrganizationDescription { get; set; }
}

You need to have a view model to represent your data on your view. 您需要一个视图模型来表示视图中的数据。

public class EditUserViewModel
{
     public int UserId { get; set; }

     public string FirstName { get; set; }

     public string LastName { get; set; }

     public int OrganizationId { get; set; }

     public IEnumerable<Organization> Organizations { get; set; }
}

public ActionResult Edit(int id)
{
     // Get the user by id
     User user = userRepository.GetById(id);

     // You can use a mapping tool here to map from domain model to view model.
     // I did it differently for the sake of simplicity

     EditAViewModel viewModel = new EditAViewModel
     {
          UserId = user.UserId,
          FirstName = user.FirstName,
          LastName = user.LastName,
          OrganizationId = user.OrganizationId,
          Organizations = organizationRepository.GetAll()
     };

     // Return the populated view model to the view    
     return View(viewModel);
}

[HttpPost]
public ActionResult Edit(EditAViewModel viewModel)
{
     if (!ModelState.IsValid)
     {
          viewModel.Organizations = organizationRepository.GetAll();

          return View(viewModel);
     }

     // If validation succeeds do what ever you have to do here, like edit in database
}

And this is how your view will look like. 这就是您的视图外观。

@model YourProject.ViewModels.Users.EditUserViewModel

@using (Html.BeginForm())
{
     <table>
          <tr>
               <td class="edit-label">First Name:</td>
               <td>
                    @Html.TextBoxFor(x => x.FirstName)
                    @Html.ValidationMessageFor(x => x.FirstName)
               </td>
          </tr>
          <tr>
               <td class="edit-label">Last Name:</td>
               <td>
                    @Html.TextBoxFor(x => x.LastName)
                    @Html.ValidationMessageFor(x => x.LastName)
               </td>
          </tr>
          <tr>
               <td class="edit-label">Organization:</td>
               <td>
                    @Html.DropDownListFor(
                         x => x.OrganizationId,
                         new SelectList(Model.Organizations, "OrganizationId", "OrganizationName", Model.OrganizationId),
                         "-- Select --"
                    )
                    @Html.ValidationMessageFor(x => x.OrganizationId)
               </td>
          </tr>
     </table>

     <button id="SaveButton" type="submit">Save</button>
}

This is how I do it. 这就是我的方法。 You can modify the code to fit in with your scenario. 您可以修改代码以适合您的方案。

I hope this helps. 我希望这有帮助。

Hi Try like this 嗨尝试这样

Model 模型

  public class CustomerModel
    {
        public int CustomerId { get; set; }

        public string customerName { get; set; }
}

View 视图

@model dynamic
@{
    Layout = null;
}

 @Html.DropDownList("CustomerId", (SelectList) ViewBag.CustomerNameID,"--Select--")

Contrller Contrller

[HttpGet]
        public ActionResult CustomerInfo()
        {

            var List = GetCustomerName();
            ViewBag.CustomerNameID = new SelectList(List, "CustomerId", "customerName");
            return View();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM