簡體   English   中英

通過在另一個模型的下拉列表中選擇一個值來填充類的一個屬性

[英]Fill one property of class by selecting one value on dropdownlist of another model

我有一個Client類,它具有一些屬性,尤其是strict_type。 另外,我還創建了另一個具有ID和名稱屬性的類Restriction。 name屬性對應於striction_type。

然后,在下拉列表中顯示數據庫中所有限制的名稱:

     @Html.ActionLink("Create New", "Create")
     @using (Html.BeginForm("AddRestrictions","Restrictions",FormMethod.Get)){
    <p> Type de restriction: 
     @Html.DropDownList("ClientRestr_type", "All")  
    </p>

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

那是我的控制器:

public ActionResult AddRestriction(string ClientRestr_type, Restriction restriction)
{
   var RestrLst = new List<string>();
   var RestrQry = from d in db.Restrictions
                               orderby d.name
                               select d.name;

   RestrLst.AddRange(RestrQry.Distinct());
   ViewBag.ClientRestr_type = new SelectList(RestrLst);
   var clients = from c in db.Restrictions select c;

   if (string.IsNullOrEmpty(ClientRestr_type))
       return View();

   else
   {
      if (ModelState.IsValid)
      {
         // Here I have maybe to find the way to solve my problem
      }
}

所以我想在Model Client的strict_type屬性中添加Restriction的name屬性。

模型客戶:

公共類客戶

{
    [Required]
    public int ID
    {
        get;
        set;
    }
    [Required]
    public string compte
    {
        get;
        set;
    }
    [Required]
    public string portefeuille
    {
        get;
        set;
    }

    [Required]
    public String restriction_type
    {
        get;
        set;
    }
    [Required]
    public Boolean etat
    {
        get;
        set;
    }
    public Boolean decision
    {
        get;
        set;
    }

型號限制:

公共類限制

{
    public int restrictionID
    {
        get;
        set;
    }
    public string name
    {
        get;
        set;
    }

}

您如何看待我的GetRestrictions()方法

私有SelectList GetRestrictions()

{

        var RestrLst = new List<string>();
        var RestrQry = from d in db.Restrictions
                       orderby d.name
                       select d.name;

        RestrLst.AddRange(RestrQry.Distinct());

        return new SelectList(RestrLst);

    }

但不幸的是,我有一個錯誤:不可能在以下行將System.Web.Mvc.SelectList轉換為MyApp.Models.Client:

model.RestrictionList = GetRestrictions();

我不明白為什么

謝謝您的幫助!

一個簡化的例子:

查看模型

public class ClientVM
{
  public Client Client { get; set; }
  public SelectList RestrictionList { get; set; }
}

控制者

[HttpGet]
public ActionResult Create()
{
  ClientVM model = new ClientVM();
  model.Client = new Client();
  model.RestrictionList = GetRestrictions(); // your code to return the select list
  return View("Edit", model);
}

[HttpGet]
public ActionResult Edit(int ID)
{
  ClientVM model = new ClientVM();
  model.Client = // call database to get client based on ID
  model.RestrictionList = GetRestrictions();
  return View(model);
}

[HttpPost]
public ActionResult Edit(ClientVM model)
{
  if (!ModelState.IsValid)
  {
    model.RestrictionList = GetRestrictions();
    return View(model);
  }
  Client client = model.Client;
  // Save and redirect
  ....
}

視圖

@model YourNamespace.ClientVM
@using (Html.BeginForm() {
  @Html.TextBoxFor(m => m.Client.ID)
  @Html.TextBoxFor(m => m.Client.compte)
  ...
  @Html.DropDownListFor(m => m.Client.restriction_type, Model.RestrictionList)
  <input type="submit" value="Save" />
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM