在asp.net mvc中的双列表框之间移动数据

时间:2014-05-07 11:26:27

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

嗨,我现在正在使用双列表框处理MVC。我想将数据从listbox1移动到listbox2。为此,我使用以下代码:

查看

<tbody>
    <tr>
        <td>
            <%:Html.HiddenFor(m=>m.objMemberAccessRights.Id) %>
            <%:Html.ListBoxFor(model =>model.objMemberAccessRights.AvailableSelected, newMultiSelectList(Model.objMemberAccessRights.AvailableNames, "Id", "InstituteName", Model.objMemberAccessRights.AvailableSelected), new { size="8"})%>
        </td>
        <td>
            <br/>
            <%:Html.ActionLink(">>", "AddNames", "MemberDetails") %>
            <br/>
            <br/>
            <%:Html.ActionLink("<<", "RemoveNames", "MemberDetails") %>
        </td>
        <td>
            <%:Html.ListBoxFor(model=>model.objMemberAccessRights.RequestedSelected,newMultiSelectList(Model.objMemberAccessRights.RequestedNames,"Id","InstituteName",Model.objMemberAccessRights.RequestedSelected), new { size="8"}) %>
        </td>
    </tr>
</tbody>

控制器

//
// GET: /MemberDetails/Create
public ActionResult Create()
{
    MemberDetailsWrapper MD = new MemberDetailsWrapper();
    MD.objMemberAccessRights = newMemberAccessRights{ AvailableNames = getAllInstituteNameList(), RequestedNames = new List<InstituteInformation>() };
    return View(MD);
}

//AddNames
public ActionResult AddNames(MemberDetailsWrapper MD, string add)
{
    ModelState.Clear();
    RestoreSavedState(MD);
    if (!add.Equals(">>"))
    {
        if (MD.objMemberAccessRights.AvailableSelected != null)
        {
            var names = getAllInstituteNameList().Where(p => MD.objMemberAccessRights.AvailableSelected.Contains(p.InstituteName));
            MD.objMemberAccessRights.RequestedNames.AddRange(names);
            MD.objMemberAccessRights.AvailableSelected = null;
        }
    }
    SaveState(MD);
    return View(MD);
}

//RemoveNames
public ActionResult RemoveNames(MemberDetailsWrapper MD, string remove)
{
    ModelState.Clear();
    RestoreSavedState(MD);
    if (!remove.Equals("<<"))
    {
        if (MD.objMemberAccessRights.RequestedSelected != null)
        {
            MD.objMemberAccessRights.RequestedNames.RemoveAll(p => MD.objMemberAccessRights.RequestedSelected.Contains(p.InstituteName));
            MD.objMemberAccessRights.RequestedSelected = null;
        }
        SaveState(MD);
        return View(MD);
    }

#regionSupportFuncs
public void SaveState(MemberDetailsWrapper MD)
{
    MD.objMemberAccessRights.SavedRequested = string.Join(",", MD.objMemberAccessRights.RequestedNames.Select(p =>p.InstituteName).ToArray());
    ////Available Names = All - Requested
    MD.objMemberAccessRights.AvailableNames = getAllInstituteNameList().Except(MD.objMemberAccessRights.RequestedNames).ToList();
}

//RestoreSavedState
public void RestoreSavedState(MemberDetailsWrapper MD)
{
    MD.objMemberAccessRights.RequestedNames = new List<InstituteInformation>();
    if (!string.IsNullOrEmpty(MD.objMemberAccessRights.SavedRequested))
    {
        string[] nameids = MD.objMemberAccessRights.SavedRequested.Split(',');
        var name = getAllInstituteNameList().Where(p =>nameids.Contains(p.InstituteName));
        MD.objMemberAccessRights.RequestedNames.AddRange(name);
    }
}
#endregion

在模型中:

public class InstituteInformation
{
    public int Id { get; set; }
    public string InstituteName { get; set; }
}

public class MemberAccessRights
{
    public int Id { get; set; }

    public List<InstituteInformation> AvailableNames { get; set; }
    public string[] AvailableSelected { get; set; }

    public List<InstituteInformation> RequestedNames { get; set; }
    public string[] RequestedSelected { get; set; }

    public string SavedRequested { get; set; }
}

此处当我点击&gt;&gt;按钮然后此按钮返回错误:

  

对象引用未设置为对象的实例。描述:一个   在执行当前Web期间发生了未处理的异常   请求。请查看堆栈跟踪以获取有关的更多信息   错误以及它在代码中的起源。例外细节:   System.NullReferenceException:未将对象引用设置为实例   一个对象。

Source Error:   
Line 170:   public void    RestoreSavedState(MemberDetailsWrapper MD)
Line 171:   { 
Line 172:       MD.objMemberAccessRights.RequestedNames = new List<InstituteInformation>();
Line 173:
Line 174:       if (!string.IsNullOrEmpty(MD.objMemberAccessRights.SavedRequested))

1 个答案:

答案 0 :(得分:0)

您的对象为空(即MD.objMemberAccessRights)。 MVC不会在没有调整的情况下在WHOLE模型中传回。特别列出

最简单的解决方案是假设列表为空,在每次请求后重建列表。根据您的代码的外观,您不会持久保存数据,但我认为您将在最后?

您需要做的是在项目移动后保留该项目,然后重新查询dB以重新创建列表。

你可以使用jquery来避免所有这些dB混乱,但这有其自身的挑战。