MVC:从我的控制器中的其他区域调用动作方法到我的视图beginform

时间:2012-02-29 14:48:20

标签: asp.net-mvc asp.net-mvc-3 entity-framework view

我正在使用MVC-Viewmodel EF模型首先,我试图在我的视图中制作2个表单,一个用于CREATE,另一个用于DELETE。 但是我无法让它工作......我收到了这个错误:

 There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Sname'.

我认为发生了这种情况,因为我在DropSlist的DeleteSubjectType中有这个代码,并且假设我在那里使用2个表单。不知何故,DeleteSubjectType动作在我的表单中工作。

如果有人可以查看我的代码,我将不胜感激:

这是我的控制器,我想在我的视图中使用我的动作beginforms:

public ActionResult CreateSubjectType()
{
    CreateViewModel model = new CreateViewModel();
    return View(model);
}
[HttpPost]
public ActionResult CreateSubjectType(CreateViewModel model)
{
    if (ModelState.IsValid)
    {
        SubjectType s = new SubjectType();
        s.Sname = model.Sname2;                            
        Arep.addsubject(s);
        Arep.save();
        return RedirectToAction("CreateSubjectType");
    }
    return View(model);
}


public ActionResult DeleteSubjectType(int id)
{
    CreateViewModel model = new CreateViewModel();
    List<SubjectType> subjectypes = Arep.getallS();
    model.SubjectTypes = new SelectList(subjectypes, "SID", "Sname");

    SubjectType SubjectTypeToDelete = Arep.getbysid(id);
    return View(model);
}

[HttpPost]
public ActionResult DeleteConfirmed2(int id)
{
    SubjectType SubjectTypeToDelete = Arep.getbysid(id);
    Arep.DeletSubjects(SubjectTypeToDelete);
    Arep.save();
    return RedirectToAction("Index");

}

这是我的观点:

@model NKI3.ViewModels.CreateViewModel
@using (Html.BeginForm("CreateSubjectType","Admin",FormMethod.Post))
{

    @Html.ValidationSummary(true)
        <fieldset>
        <legend>Skapa ny utvärderingsobjekt</legend>
        <div class="editor-label">
        @Html.LabelFor(model => model.Sname2, "Välj ny utvärderingsobjekt")
        </div>
        <div class="editor-field">
        @Html.EditorFor(model => model.Sname2)
        @Html.ValidationMessageFor(model => model.Sname2)
        </div>
        <p>
        <input type="submit" value="Lägg till" />
        </p>
        </fieldset>

    }  
         @using (Html.BeginForm("DeleteSubjectType","Admin",FormMethod.Post))
         {   

            <fieldset>  
            <legend>Ta bort utvärderingsobjekt</legend>
            <div class="editor-label">
            @Html.LabelFor(model => model.Sname, "Befintliga Utvärderingsobjekt")
            </div>
            <div class="editor-field">
            @Html.DropDownListFor(f => f.Sname, Model.SubjectTypes)
            @Html.ValidationMessageFor(model => model.Sname)
            </div>
            <p>
            <input type="submit" value="Ta bort" />
            </p>    
            </fieldset>
         }

    <div>
        @Html.ActionLink("Tillbaka", "Index")
    </div>

提前致谢!

2 个答案:

答案 0 :(得分:1)

根据您的问题,您所显示的两个GET操作中的哪一个与此视图相关联尚不清楚:是CreateSubjectType操作还是DeleteSubjectType操作?或者甚至可能还有其他一些你没有表现过的动作?

如果我们认为它是CreateSubjectType.cshtml,则表示您遇到问题,因为在此视图中,您尝试在Sname属性的删除表单中创建一个下拉列表。除了在CreateSubjectType中,您从未像SubjectTypes操作中那样将视图模型的DeleteSubjectType属性初始化为SelectList。

因此,请确保正确初始化此属性:

public ActionResult CreateSubjectType()
{
    CreateViewModel model = new CreateViewModel();
    List<SubjectType> subjectypes = Arep.getallS();
    model.SubjectTypes = new SelectList(subjectypes, "SID", "Sname");
    return View(model);
}

此外,您的删除表单似乎已发布到DeleteSubjectType操作:

@using (Html.BeginForm("DeleteSubjectType","Admin",FormMethod.Post))

除了在你的控制器上我看到:

[HttpPost]
public ActionResult DeleteConfirmed2(int id)

而不是:

[HttpPost]
public ActionResult DeleteSubjectType(int id)

我猜你被迫这样做是为了避免你用同名的GET动作发生的重载解决冲突。在这种情况下,你可以这样做:

[HttpPost]
public ActionResult DeleteSubjectType(int id, FormCollection fc)

答案 1 :(得分:0)

我认为你对MVC有点误解。您收到的错误发生在这一行:

@Html.DropDownListFor(f => f.Sname, Model.SubjectTypes)

原因是当您渲染视图时,您没有填充Model.SubjectTypes。因此,我将向您介绍系统如何解释您的代码:

向您的控制器发出请求,请求Action&#34; CreateSubjectType&#34;。您将模型设置为新的CreateViewModel并返回您列出的视图。在渲染视图时,Model.CreateSubjectType为null。您可以调试并在我上面显示的行上放置一个断点,以便自己查看。您需要做的下拉工作是将这两行移动到您的CreateSubjectType操作中:

List<SubjectType> subjectypes = Arep.getallS();
model.SubjectTypes = new SelectList(subjectypes, "SID", "Sname");

现在你拥有它的方式,只有在你删除表格后才会执行此操作。