在mvc 4中从列表框中选择(全部在一个视图中)时显示要编辑的数据

时间:2014-01-10 07:28:32

标签: c# asp.net-mvc asp.net-mvc-4 listbox

我是MVC(MVC4)的新手,我有这个问题: 我需要一个视图来显示左侧的列表框数据,并在此视图右侧添加/编辑表单。 问题是:当我在列表框中选择然后单击“编辑”,没有数据显示在编辑表单上。 screen for this question 我的观点和代码如下:

我的视图名称 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;
    }

请帮帮我。 (对不起,我的英语不好)

1 个答案:

答案 0 :(得分:0)

单击“编辑”时,需要调用GetZone。我相信截至目前,您的表单只是对用于加载页面的相同操作进行POST。

Add a new action method to return the selected zone detail

在您的情况下,您可以开始使用jQuery ajax并在右侧加载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);    
   });
  });
});