ASP.NET MVC 4 - ListBoxFor,在ActionLink中发送selectedValue

时间:2014-07-18 13:57:43

标签: asp.net-mvc-4 razor selectedvalue html.actionlink html.listboxfor

我有一个模型列表。我想检索listBoxSelectedValue以在我的actionLink中发送它以进行编辑。

这是我的观点:

@using (Html.BeginForm())
{
    @Html.ListBoxFor(a => a.SelectedApplis, new SelectList(ViewBag.Applis,"ID","Name", Model.SelectedApplis))<br/>
    @Html.ActionLink("Add","Create","Application")<br/>
    @Html.ActionLink("Edit","Edit","Application", null, new { listAppId = Model.SelectedApplis})<br/>
    @Html.ActionLink("Delete","Delete","Application")<br/>
}  

我创建了一个类&#34; ListBoxApplication&#34;使用List将包含ListBox的selectedValue。

public class ListBoxApplication
{
    public IEnumerable<int> SelectedApplis { get; set; }

    public ListBoxApplication()
    {
        SelectedApplis = new List<int>();
    }
}

我有2个控制器:应用程序和主页

在HomeController中,我创建了包含List的模型ListBoxApplication。在我的ViewBag.Applis中,我拥有所有的ApplicationModel。

    public ActionResult Index()
    {
        ListBoxApplication listeApplis = new ListBoxApplication();

        ViewBag.Applis = ApplicationModels.GetListApplications();
        return View(listeApplis);
    }

在我的ApplicationController中:

    public ActionResult Edit(ListBoxApplication listAppId)
    {
        // I WANT TO RETRIEVE MY listAppId HERE, but it is always 'null'
        return View();
    }

所以我认为我的问题在于actionLink:

@Html.ActionLink("Edit","Edit","Application", null, new { listAppId = Model.SelectedApplis})

Me Edit Method不是实际控制器(Home / Index)。我需要将我的listBox中的ListBox的selectedValue发送到(Application / Edit)。

listAppId始终为&#39; null&#39;。它没有检索到值...我的actionLink中有错误吗?

感谢您提前

1 个答案:

答案 0 :(得分:0)

我不相信动作链接会触发回发到服务器。试试这个:

    @Html.ActionLink("Delete","Delete","Application")<br/>
    @Html.ActionLink("Add","Create","Application")<br/>

    @using (Html.BeginForm("Detail","Application"))
    {
        @Html.ListBoxFor(a => a.SelectedApplis, new SelectList(ViewBag.Applis)) //not sure what the other params you had here were for, but it should work like this
        <br/>
        <input type="submit" name="Edit" value = "Edit"/>
        @*added in response to comment*@
        <input type="submit" name="Delete" value = "Delete"/>
        <input type="submit" name="Add" value = "Add"/>
    }  

如果您计划将所有这些按钮发回服务器,您还可以使用ajax(和javascript)来实现相同的目标,而无需为每个按钮写出表单。两种方式都可以正常工作,但多种形式在技术上更容易。

public ActionResult Detail(ListBoxApplication listAppId, bool Edit, bool Add, bool Delete)
{
    if(//check your bools here){
    }
    return View();
}