如何在MVC中构建表单?

时间:2016-08-29 13:22:43

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

我正在尝试在我的MVC应用程序中构建一个视图('Editor')中的表单,但是当用户按下Submit按钮时我收到错误。我的表单有一个CKEditor控件。

我的编辑器视图

@model MyModel

@using (Html.BeginForm("SubmitForm", "Edit"))
{
    <textarea name="editor1" id="editor1" rows="400" cols="100">
    @Model.Text
    </textarea>
    <input type="submit" value="Submit" />
}

我的控制器

[HttpPost, ValidateInput(false)]
public ActionResult SubmitForm(FormCollection collection)
{
    string sText = collection[0].ToString();

    // save string from CKEditor

    return View("Editor");  //<-- I get the error here.
}

以下是从我的控制器返回时的错误。如果我返回View(),我会得到同样的错误。

The view 'SubmitForm' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Edit/SubmitForm.aspx
~/Views/Edit/SubmitForm.ascx
~/Views/Shared/SubmitForm.aspx
~/Views/Shared/SubmitForm.ascx
~/Views/Edit/SubmitForm.cshtml
~/Views/Edit/SubmitForm.vbhtml
~/Views/Shared/SubmitForm.cshtml
~/Views/Shared/SubmitForm.vbhtml 

如果我返回我的模型而不是'return(MyModel)',我得到一个不同的错误,'Model.NullReferenceException'在@ Model.Text

<textarea name="editor1" id="editor1" rows="400" cols="100">
@Model.Text
</textarea>

1 个答案:

答案 0 :(得分:1)

看起来您的视图位于错误的位置,并且在运行时使用提交表单操作时无法找到它!在跟踪中,您可以查看搜索的位置。自动执行此操作的最佳方法是右键单击操作并选择“添加视图”菜单项。除了那个表格看起来还不错!