我的观点不要绑定viewModel MVC4

时间:2013-02-01 04:22:37

标签: asp.net-mvc-4 model-binding html.beginform

很抱歉,如果我没有看到任何错误,但我的View不会绑定我的ViewModel。我调试了很多次,当断点位于操作行上时,模型中没有数据。 该项目的其他观点工作正常。

动作:

[HttpPost]
public ActionResult Index_Funcionario(FuncionariosViewModel viewModel)
{
...

查看

@model ZebraStudio.Models.FuncionariosViewModel

@{
ViewBag.Title = "Index_Funcionario";
}


<div class="main-field">
<div class="linha-cabecalho">
    <div class="aba">Consulta</div>
    <div class="area">Funcionários > Consultar</div>
</div>
<div class="sub-field">

    <div class="search-field">
        @using (Html.BeginForm())
        { 


            <div class="floatleft">
            @Html.RadioButton("TipoFiltro", "1", new { @class = "radioFloat" })
            <div >Código: @Html.TextBoxFor(model => model.cod_Usuario, new { @class = "customeditor"})</div>
            </div>

            <div class="floatleft">
            <div class="radioFloat">@Html.RadioButton("TipoFiltro", "2", new { @class = "radioFloat" })</div> 
            <div>Nome: @Html.TextBoxFor(model => model.nome, new { @class = "customeditor"})</div>
            </div>

            <div class="floatleft">
            @Html.RadioButton("TipoFiltro", "3", true, new { @class = "radioFloat" })
            <div class="inline">Listar Todos.</div>
            </div>
                <br />


            <input type="submit" value="Buscar" class="searchbutton"/>

        }
        </div>



    <table class="search-result">
        <tr>
            <th>Código</th>
            <th>Nome</th>
            <th>Email</th>
            <th>Tipo de Usuário</th>
            <th>Ações</th>
        </tr>
        @foreach (var usuario in Model.usuarios)
        {
            <td>@usuario.UserId</td>
            <td>@usuario.Nome</td>
            <td>@User.Identity.Name</td>
            <td>@foreach (var role in Roles.GetRolesForUser())
                {
                    @role
                }
            </td>
            <td></td>
        }


    </table>


</div>

视图模型

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace ZebraStudio.Models
{
public class FuncionariosViewModel
{
    public IEnumerable<Usuario> usuarios;

    public Usuario usuario;

    public string nome;

    [DataType(DataType.EmailAddress)]
    public string Email;

    public string password;

    public int opcao_Selecionada;

    public int cod_Usuario;

}
}

有什么不对吗?请帮助我。

1 个答案:

答案 0 :(得分:0)

ASP.Net MVC模型绑定不支持字段 您需要将这些更改为属性。

您可以使用自动实现的属性:

public string Email { get; set; }