Asp.net mvc 4 过帐时模型的属性变为空

Asp.net mvc 4 过帐时模型的属性变为空,asp.net-mvc-4,Asp.net Mvc 4,我是MVC新手。现在,我尝试使用一个简单的MVC演示,将客户的信息打印到屏幕上并更新,然后发送回数据库 我不知道为什么客户的ID变为空,而其他的都很好 我想在屏幕上显示客户的ID,但我不想让用户编辑它以发布到我的数据库。我已经研究了几个小时了 我的代码: Customer.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataA

我是MVC新手。现在,我尝试使用一个简单的MVC演示,将客户的信息打印到屏幕上并更新,然后发送回数据库

我不知道为什么客户的ID变为空,而其他的都很好

我想在屏幕上显示客户的ID,但我不想让用户编辑它以发布到我的数据库。我已经研究了几个小时了

我的代码:

Customer.cs

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

namespace PeopleManagement.Models
{
    public class Customer
    {
        [Required]
        public string Id { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

        public Customer()
        {

        }
        public Customer(string id, string name, int age)
        {
            Id = id;
            this.Name = name;
            this.Age = age;
        }
    }
}
using System.Collections.Generic;
using System.Web.Mvc;
using PeopleManagement.Models;

namespace PeopleManagement.Controllers
{
    public class HomeController : Controller
    {
        public List<Customer> CustomersList { get; private set; } = new List<Customer>(5);

        [HttpGet]
        public ActionResult Index()
        {
            CustomersList.Add(new Customer("ID_1", "Name_1", 1));
            CustomersList.Add(new Customer("ID_2", "Name_2", 2));
            CustomersList.Add(new Customer("ID_3", "Name_3", 3));

            ModelState.Clear();
            return View(CustomersList);
        }

        [HttpPost]
        public ActionResult Index(List<Customer> postbackCustomers)
        {
            if (!ModelState.IsValid)
                return View(CustomersList);

            CustomersList = postbackCustomers;

            return View(CustomersList);
        }
    }
}
Index.cshtml

@using System.Web.UI.WebControls
@using PeopleManagement.Models
@model IList<Customer>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>First MVC Application</title>
        <link href="@Url.Content("~/Content/css/customized-table.css")" rel="stylesheet" type="text/css" />
    </head>

    <body style="max-width:100%; max-height:100%">

        <div id="pageTitle" style="text-align:center; color:red; font-size:24px; font-weight:bold;">Customer Management</div>
        @using (@Html.BeginForm("Index", "Home", FormMethod.Post))
        {
            <div id="tablePanel" style="padding-top: 15px">

                <table class="customized_table" border="1">
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Age</th>
                    </tr>

                    @{
                        for (var i = 0; i < Model.Count; i++)
                        {
                            <tr>
                                <td>
                                    @Html.HiddenFor(model => model[i].Name);
                                </td>
                                <td>
                                    @Html.TextBoxFor(model => model[i].Name, new {@style = "min-width:100%; text-align:center", @disable = "true"})
                                </td>
                                <td>
                                    @Html.TextBoxFor(model => model[i].Age)
                                </td>
                            </tr>
                        }
                    }
                </table>

            </div>

            <div>
                <p><input type="submit"/></p>
            </div>

        }

    </body>

</html>

<script>

</script>
@使用System.Web.UI.WebControls
@使用人员管理模型
@模型IList
第一个MVC应用程序
客户管理
@使用(@Html.BeginForm(“Index”,“Home”,FormMethod.Post))
{
身份证件
名称
年龄
@{
对于(var i=0;imodel[i].Name);
@Html.TextBoxFor(model=>model[i].Name,新{@style=“min-width:100%;text-align:center”,@disable=“true”})
@Html.TextBoxFor(model=>model[i].Age)
}
}

}
HomeController.cs

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

namespace PeopleManagement.Models
{
    public class Customer
    {
        [Required]
        public string Id { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

        public Customer()
        {

        }
        public Customer(string id, string name, int age)
        {
            Id = id;
            this.Name = name;
            this.Age = age;
        }
    }
}
using System.Collections.Generic;
using System.Web.Mvc;
using PeopleManagement.Models;

namespace PeopleManagement.Controllers
{
    public class HomeController : Controller
    {
        public List<Customer> CustomersList { get; private set; } = new List<Customer>(5);

        [HttpGet]
        public ActionResult Index()
        {
            CustomersList.Add(new Customer("ID_1", "Name_1", 1));
            CustomersList.Add(new Customer("ID_2", "Name_2", 2));
            CustomersList.Add(new Customer("ID_3", "Name_3", 3));

            ModelState.Clear();
            return View(CustomersList);
        }

        [HttpPost]
        public ActionResult Index(List<Customer> postbackCustomers)
        {
            if (!ModelState.IsValid)
                return View(CustomersList);

            CustomersList = postbackCustomers;

            return View(CustomersList);
        }
    }
}
使用System.Collections.Generic;
使用System.Web.Mvc;
使用人员管理模式;
命名空间PeopleManagement.Controllers
{
公共类HomeController:控制器
{
公共列表CustomerList{get;private set;}=新列表(5);
[HttpGet]
公共行动结果索引()
{
添加(新客户(“ID_1”,“名称_1”,1));
添加(新客户(“ID_2”,“Name_2”,2));
添加(新客户(“ID_3”,“名称_3”,3));
ModelState.Clear();
返回视图(CustomerList);
}
[HttpPost]
公共行动结果索引(列出回发客户)
{
如果(!ModelState.IsValid)
返回视图(CustomerList);
CustomersList=postbackCustomers;
返回视图(CustomerList);
}
}
}

有人能帮忙吗?

在MVC中,如果你想从视图中获得一些值,你必须先在视图中获得该值。您似乎没有在视图中输入Id

像这样更改视图代码

for (var i = 0; i < Model.Count; i++)
            {
                <tr>
                    <td>
                        @Html.HiddenFor(model => model[i].Id)
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model[i].Name, new { @style = "min-width:100%; text-align:center", @disable = "true" })
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model[i].Age)
                    </td>
                </tr>
            }
for(变量i=0;imodel[i].Id)
@Html.TextBoxFor(model=>model[i].Name,新{@style=“min-width:100%;text-align:center”,@disable=“true”})
@Html.TextBoxFor(model=>model[i].Age)
}

希望这有帮助

实际上,您没有在index.html视图上显示ID,客户ID在您的示例中具有价值。您应该尝试在for each循环中以只读模式显示它

@Html.DisplayFor(model => model[i].Id)

除了不包括以下答案(1)中所示的隐藏输入外,属性
Name
不应同时包含隐藏输入和文本框。我假设您试图禁用文本框,而
@disable=“true”
将不起作用(其
disabled=“disabled”
),但只需将其设置为
readonly=“readonly”
,并删除隐藏的输入。(2)
ModelState.Clear()是无意义的-在GET方法中,没有向
ModelState
添加任何内容。(3)
返回视图(CustomerList)没有意义-如果模型无效,为什么返回空列表。当我使用@Html.DisplayFor时,如何保护m ID?因为现在,我可以使用浏览器的调试器更改提交数据?