2014-01-10 20 views
0

我是新手在MVC(MVC4),我有這個問題: 我需要一個視圖來顯示列表框左側的數據和表單添加/在此視圖的右側編輯。 問題是:當我在列表框上選擇,然後單擊「編輯」,沒有數據顯示在編輯窗體上。 screen for this question 我的視圖和代碼波紋管:顯示數據進行編輯時,從列表框中選擇(全部在一個視圖中)mvc4

我的視圖名稱AddZone(Addzone.cshtml):

@model CTN_MVC.Models.Zone 

@{ 
    ViewBag.Title = "AddZone"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 
<h2>Zone Management 
</h2> 
<p></p> 

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true)  
    <div style="float: right"> 
     <fieldset> 
      <legend>AddZone</legend> 

      <div class="editor-label"> 
       @Html.LabelFor(model => model.Name) 
      </div> 
      <div class="editor-field"> 
       @Html.EditorFor(model => model.Name) 
       @Html.ValidationMessageFor(model => model.Name) 
      </div> 

      <div class="editor-label"> 
       @Html.LabelFor(model => model.ParentID) 
      </div> 
      <div class="editor-field"> 
       @Html.DropDownList("parentzone", new SelectList(new CTN_MVC.Controllers.AdminController().ShowParentZone((string)ViewBag.ParentZoneSelect), "Value", "Text"), " -- Choice Zones -- ", new { style = "width:312px" }) 
      </div> 
      <p> 
       <button name="zoneaction" title="Update" value="zone_add" >Update</button> 
      </p> 
     </fieldset> 
    </div> 
    <div style="float: left"> 
     @{Html.RenderPartial("ListZones");} 
    </div> 
} 

這裏是ListZone.cshtml

@Html.ListBox("listzone", new CTN_MVC.Controllers.AdminController().ShowListZone((string[])ViewBag.ZoneSelect), new { size = "15", multiple = "multiple", style = "width:450px" }) 
<br /> 
<br /> 
<table> 
    <tr> 
     <td> 
      <button title="Sửa" value="zone_edit" name="zoneaction">Edit</button> 
     </td> 
     <td> 
      <button title="Xóa" value="zone_del" name="zoneaction">Delete</button> 
     </td> 
    </tr> 
</table> 

和我的控制器代碼:

[Authorize] 
    public ActionResult AddZone() 
    { 
     return View(); 
    } 

    [HttpPost] 
    [Authorize] 
    public ActionResult AddZone(Zone input, string zoneaction, string[] listzone, string parentzone) 
    { 
     if (zoneaction == "zone_del") 
     { 
      foreach (string id in listzone) 
      { 
       mghelper.Delete<Zone>(TableNames.Zone, "ZoneID", id); 
      } 
      return RedirectToAction("AddZone"); 
     } 
     else if (zoneaction == "zone_edit") 
     { 
      input = mghelper.GetInfo<Zone>(TableNames.Zone, "ZoneID", listzone[0])[0]; 
      ViewBag.ParentZoneSelect = input.ParentID.ToString();     
      return View("AddZone", input); 
     } 
     else 
     { 
      if(input.ZoneID > 0) 
      { 
       input.ParentID = Utility.ConvertToInt(parentzone); 
       input.DateCreate = DateTime.UtcNow; 
       input.AdminID = CurrentAdmin._id; 
       mghelper.Updates<Zone>(input, TableNames.Zone); 
      } 
      else 
      { 
       List<Zone> mtinfo = mghelper.GetLast<Zone>(TableNames.Zone, "DateCreate", 0); 
       if (mtinfo == null || mtinfo.Count < 1) 
        input.ZoneID = 1; 
       else 
        input.ZoneID = mtinfo[0].ZoneID + 1; 
       input.ParentID = Utility.ConvertToInt(parentzone); 
       input.DateCreate = DateTime.UtcNow; 
       mghelper.Insert<Zone>(input, TableNames.Zone);      
      } 
      return RedirectToAction("AddZone"); 
     } 

    } 

    public string ZoneSelect { get; set; } 
    public string ParentZoneSelect { get; set; } 
    public IEnumerable<SelectListItem> ShowListZone(string[] id) 
    { 
     if (id == null) 
     { 
      id = new string[1]; 
      id[0] = "0"; 
     } 

     var allFlavors = GetListZone(id).Select(f => new SelectListItem 
     { 
      Value = f.ZoneID.ToString(), 
      Text = f.ZoneName, 
      Selected = f.isSelected 
     }); 
     return allFlavors;    
    } 
    public class SelectZone 
    { 
     public string ZoneName { get; set; } 
     public string ZoneID { get; set; } 
     public bool isSelected { get; set; } 
    } 
    public IEnumerable<SelectListItem> ShowParentZone(string id) 
    { 
     if (id == null) id = "0"; 
     string[] lid = new string[1]; 
     lid[0] = id; 
     var allFlavors = GetListZone(lid).Select(f => new SelectListItem 
     { 
      Value = f.ZoneID.ToString(), 
      Text = f.ZoneName, 
      Selected = f.isSelected 
     }); 
     return allFlavors; 
    } 

    public List<SelectZone> GetListZone(string[] lid) 
    { 
     var query = Query.EQ("ParentID", 0); 
     List<Zone> lz = mghelper.GetByCondition<Zone>(TableNames.Zone,query, 0,false); 
     List<SelectZone> lsz = new List<SelectZone>(); 
     foreach (Zone z in lz) 
     { 
      SelectZone sl = new SelectZone(); 
      sl.ZoneName = z.Name; 
      sl.ZoneID = z.ZoneID.ToString(); 
      bool checksl = false; 
      foreach (string id in lid) 
      { 
       if (z.ZoneID.ToString() == id) 
       { 
        checksl = true; 
       }     
      } 
      sl.isSelected = checksl; 
      lsz.Add(sl); 
      //RecruiListZone(lsz, z.ZoneID, lid); 
     } 
     return lsz; 
    } 

請幫幫我。 (對不起,我的英文不好)

+0

這可能是因爲數據綁定的。檢查數據綁定,如果可以的話。當您編輯表單時,它會從數據庫的相關字段中獲取數據。 – CodeXerox

+0

上**公共的ActionResult AddZone是的,我結合數據**如果 ' 否則(zoneaction == 「zone_edit」) { 輸入= mghelper.GetInfo (TableNames.Zone 「了zoneid」,listzone [0]) [0]; ViewBag.ParentZoneSelect = input.ParentID.ToString(); return View(「AddZone」,input); } ' 但數據不綁定到編輯(添加)窗體。 – user3073441

回答

0

當你點擊編輯時,你需要調用GetZone。我相信到現在爲止,你的表單只是簡單地使用POST來加載頁面。

Add a new action method to return the selected zone detail

在你的情況,你可以開始使用jQuery阿賈克斯,並在右側裝載Partial view

下面是sample code加載區細節

控制器

[HttpPost] 
public ActionResult GetZone(string id) 
{ 
    //load your model 
    return PartialView("ZoneDetail.cshtml",model); 
} 

jQuery的

$(document.ready(function(){ 
$('#edit-zone').click(function(){ 
    $.post('/Zone/GetZone',{id:selectedId}, function(data){  
     $('#zone-detail-container').html(data);  
    }); 
    }); 
}); 
+0

謝謝,但我只需要在這個視圖上綁定數據(將數據綁定到其他視圖很容易):) – user3073441

相關問題