MVC:404错误,找不到ViewModel

时间:2015-11-18 10:36:33

标签: html asp.net-mvc-4 model-view-controller

当我尝试将按钮插入表格时,我是MVC的新手,并且遇到了这个令人沮丧的错误。

我在EditorTemplates中使用我的.cshtml ViewModel:

@model EditorForSample.Models.ProductViewModel


<tr>
    <td>@Html.CheckBoxFor(m => m.Selected)</td>
    <td colspan="1">
        @Model.Name
        @Html.HiddenFor(m => m.Name)
    </td>
    <td>
        @Model.Password
        @Html.HiddenFor(m => m.Password)
    </td>
    <td>
        @Html.RadioButtonFor(e => e.Locked, true)</td>
    <td>
        @Html.RadioButtonFor(e => e.Locked, false)</td>
</tr>

对于模型:

public class ProductViewModel
{
    public bool Selected{ get; set; }
    public string Name { get; set; }
    public string Password { get; set; }
 public bool Locked { get; set; }
}

我在.cshtml中遇到404错误,即使我无法确定语法错误的原因。

编辑:好的,事实证明,当我手动更改URL时,上面实际上确实有效,而我的问题是其中一个路由随意改变自己的方式并尝试重定向到模型视图CSHTML。

1 个答案:

答案 0 :(得分:0)

您应该将viewmodel发送到这样的视图。

ProductViewModel productViewModel= new ProductViewModel();

//populate productViewModel with values

View(productViewModel);

还要确保在文件顶部添加了正确的命名空间

像这样:

using Project.Models; 

编辑:

为相同的模型属性添加Radiobutton的示例:

@Html.RadioButtonFor(model => model.Selected, "false") 
@Html.RadioButtonFor(model => model.Selected, "true")

在控制器中它应该是:

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return Content("IsFemale: " + model.Selected);
    }

我认为这会激励你。当然你必须根据你的需要修改它。