2012-03-01 62 views
0

我有這個問題,我想爲每週的每一天做7個下拉菜單。 在每個下拉列表中,我希望添加相同的數據。使用ViewModel在MVC3 C#中創建下拉菜單,並在POST上創建簡單的模型綁定。

我的視圖模型:

public class WeekDienstCreateViewModel 
{ 
    public WeekDienst weekDienst {get; set;} 
    public List<DienstPerWeekDienst> diensten { get; set; } 

    public WeekDienstCreateViewModel() { } 
} 

我在控制器中創建方法: 就像你看到的我添加的一切,除了媒體鏈接這DienstId是想與我的下拉菜單添加。

public ActionResult Create(int id) 
     { 
      WeekDienst wd = _service.FindWeekDienst(id); 
      WeekDienstCreateViewModel vm = new WeekDienstCreateViewModel(); 

      vm.diensten = new List<DienstPerWeekDienst>(); 
      vm.weekDienst = wd; 

      for (int i = 1; i <= 7; i++) 
      { 
       DienstPerWeekDienst dpwd = new DienstPerWeekDienst(); 
       dpwd.volgnummer = i; 
       dpwd.WeekDienstId = wd.Id; 

       vm.diensten.Add(dpwd); 
      } 
      ViewBag.Diensten = _service.DienstenList(wd.AfdelingId); 

      return View(vm); 
     } 

類:

public class DienstPerWeekDienst 
    { 
     [Key] 
     public int Id { get; set; } 

     [Required] 
     public int WeekDienstId { get; set; } 

     [Required] 
     public int DienstId { get; set; } 

     [Required] 
     [Range(1, 7)] 
     public int volgnummer { get; set; } 

     [ForeignKey("WeekDienstId")] 
     public virtual WeekDienst WeekDienst { get; set; } 

     [ForeignKey("DienstId")] 
     public virtual Dienst Dienst { get; set; } 

     public virtual ICollection<WeekDienst> WeekDiensten { get; set; } 
    } 

public class WeekDienst 
    { 
     [Key] 
     public int Id { get; set; } 

     [Required] 
     public int AfdelingId { get; set; } 

     [Required] 
     [StringLength(5, ErrorMessage = "Value for {0} cannot exceed {1} characters.")] 
     [RegularExpression(@"^[a-zA-Z0-9]{5}$", ErrorMessage = "Verplicht 5 cijfers lang.")] 
     public string code { get; set; } 

     [DisplayName("Template")] 
     public bool template { get; set; } 

     [ForeignKey("AfdelingId")] 
     public virtual Afdeling Afdeling { get; set; } 
    } 

在我看來,我希望創建7個下拉菜單這裏我把我的所有 「Diensten」(類丁斯特,FK在DienstPerWeekDienst)。當我選擇1時,我希望將「DienstId」添加到「DienstPerWeekDienst」類中​​。

所以,在我看來,我得到這個:

  @foreach (var day in Model.diensten)     
      { 
        var currentDay=day; 
        @Html.DropDownListFor(currentDropDown=>currentDay, new SelectList(ViewBag.Diensten, "Value", "Text"))     
      } 

我想回發選擇了「Diensten」,創造了「WeekDienst」,但現在我只是張貼空「DienstPerDienstWeekCreateViewModel」。我怎樣才能解決這個問題?

由於提前

FIX(感謝溼婆戈帕爾)

我解決了這個問題做:

@for (int i = 0; i < @Model.diensten.Count; i++) 
       { 
        @Html.HiddenFor(m => (m.diensten[i].volgnummer)) 
        @Html.HiddenFor(m => (m.diensten[i].WeekDienstId)) 
        @Html.DropDownListFor(m=> (m.diensten[i].DienstId), new SelectList(ViewBag.Diensten, "Value", "Text")) 
       } 
+0

請讓我知道,如果你看到我給出的解決方案後仍然面臨問題。 – 2012-03-01 09:40:23

回答

1

您可以嘗試使用

@foreach (var day in Model.diensten)     
{ 
     var currentDay=day;    
     @Html.DropDownListFor(currentDropDown=>currentDay, new SelectList(ViewBag.Diensten, "PropertyName_Holding_Value", "PropertyName_Holding_DisplayText"), new { })     
} //This uses the Lambda Expression. Your dropdown Name/Id would be 1,2,3 etc. based on currentDay value. 

OR

@foreach (var day in Model.diensten)     
{ 
     var currentDay=day; 
     var dropdownName=string.Format("diensten[{0}]",day-1); //If you want to model bind the selected dropdown value to input entity in POST request. The final dropdownName format should match the hierarchy of the property inside input entity/object. Even without this name formation, you can still POST the selected value back using Jquery/Javascript. 
     @Html.DropDownList(dropdownName, new SelectList(ViewBag.Diensten, "PropertyName_Holding_Value", "PropertyName_Holding_DisplayText"), new {})     
} // 

注意的價值郵報整版回/模型綁定提交: 爲了能夠綁定/ POST回值模型,服務器,對應的屬性html元素名稱應該如下呈現:假設如果顯示Employee.Department.Name,則顯示文本框的名稱,在View中顯示部門名稱應匹配Department_ReferenceName_Inside_Employee.Name以進行模型綁定。

型號: 公共類員工 { 公衆詮釋標識{獲得;組; } public string Name {get;組; } public string City {get;組; } 公共部門EmpDepartment {get;組; } public List SubOrdinates {get;組; } } public class Department { public string Name {get;組; } }

控制器:

public class HomeController : Controller 
    { 
     public ActionResult Index() 
     { 
      ViewBag.Message = "Welcome to ASP.NET MVC!"; 

      //Prepare the model and send it to the view 
      Employee emp = new Employee { EmpDepartment = new Department { Name = "IT" } }; 
      emp.SubOrdinates = new List<Employee> { new Employee { Name = "Emp1" }, new Employee { Name = "Emp2" } }; 
      return View(emp); 
     } 

     [HttpPost] 
     public ActionResult Index(Employee emp) 
     { //Put a break-point here and see how the modified values in view are flowing into emp.. 
      return View(emp); 
     } 
     public ActionResult About() 
     { 
      return View(); 
     } 
    } 

檢視:

@model MvcApplication.Models.Employee 
@using (Html.BeginForm()) 
{ 

    @Html.TextBoxFor(m => m.EmpDepartment.Name) 
    @Html.LabelForModel("SubOrdinates :") 
    for (int i = 0; i < @Model.SubOrdinates.Count; i++) 
    { 
      @Html.TextBoxFor(m => (m.SubOrdinates[i].Name)) 
    } 
<input type="submit" name="name" value="Submit" /> } 

ViewSource/PageSource: 上述文本框的語法將呈現爲:

<input id="EmpDepartment_Name" name="EmpDepartment.Name" type="text" value="IT" /> <!--See above html : name=EmpDepartment.Name --> 
<label for="">SubOrdinates :</label> 
<input id="SubOrdinates_0__Name" name="SubOrdinates[0].Name" type="text" value="Emp1" /> 
<input id="SubOrdinates_1__Name" name="SubOrdinates[1].Name" type="text" value="Emp2" /> <!--See above html for how collection item Name(s) are being renderd by view engine-->  
<input type="submit" name="name" value="Submit" /> 
+0

謝謝我現在可以顯示我的「Diensten」,但是我現在要將ViewModel發佈到7「DienstPerWeekDienst」列表中。現在我只返回一個空的viewmodel。 – Rmon 2012-03-01 09:47:08

+0

@ user1242203:您是否在頁面上使用按鈕進行發佈或使用任何基於發佈的腳本(jQuery/AJAX等)?如果您使用「提交」按鈕POST,那麼請再次通過我的第二個代碼片段中的評論,您將會知道POST值返回到服務器需要做些什麼。如果您發現我的回答對您有幫助,請不要忘記註冊並標記爲答案:)。 – 2012-03-01 09:55:26

+0

我知道該怎麼做,但不知道該怎麼做。我很想投票給你,但我剛開始使用這個網站(我沒有選擇;))當我獲得15個聲望時,我會投票給你! – Rmon 2012-03-01 10:04:41

0
@foreach (var day in Model.diensten) 
{ 
    var currentDay = day; 
    @Html.DropDownListFor(x => currentDay, new SelectList(ViewBag.Diensten, "Value", "Text"), new { @id = "DienstList" }) 
} 
0
List<MvcApplication1.Models.Country> cntry = db.Countries.ToList(); 
SelectListItem sss = new SelectListItem(); 
List<SelectListItem> sltst = new List<SelectListItem>(); 
sss.Text = "Select"; 
sss.Value = "0"; 
sltst.Add(sss); 
foreach (MvcApplication1.Models.Country s in cntry){ 
SelectListItem s1 = new SelectListItem(); 
s1.Text = s.Country1; 
s1.Value = Convert.ToString(s.Id); 
sltst.Add(s1);} 
@Html.DropDownList("country", sltst, new { @id = "country" })