UpdateModel not working (MVC)

本文关键字:MVC working not UpdateModel | 更新日期: 2023-09-27 18:01:05

我一直在学习书呆子晚餐教程。我在这个基础上创建了自己的项目,但我很难让UpdateModel正常工作。这看起来很复杂,所以我会在下面详细解释。

在服务器Controller.cs:中

public ActionResult Create(FormCollection formValues)
{
    Server server = new Server();
    try
    {
        UpdateModel(server);  <----- This is not working
        // server.Name = "testAdd";
        // server.OS = "2008 R2";
        serverRepository.Add(server);
        serverRepository.Save();
        return RedirectToAction("Details", new { id = server.ServerID });
     }
     catch
     {
     **not important**
     }

如果我尝试使用UpdateModel(Server(,则不会将任何内容保存到数据库表中。但是,如果我注释掉那一行,并在设置服务器的代码中使用注释行。名称和服务器。OS,这确实会将其保存到表中。但是,任何其他正在发布的表单输入都不会保存。。

例如,如果我明确设置了服务器。名称和服务器。操作系统,但随后通过表单设置其他属性,如LastBackedUp和Model,表单中设置的任何属性都不会保存到数据库表中,当页面重定向时,它们也不会反映在"详细信息"视图中。

以下是"Details"GET方法的代码,也在ServersController.cs:中

public ActionResult Details(int id)
{
    Server server = serverRepository.GetServer(id);
    RackInfo rackInfo = rackRepository.GetRack(id);
    if (server == null)
        return View("NotFound");
    else
        return View(new ServerDetailViewModel(rackInfo, server));
}

基本上,在创建并保存新服务器后,它应该加载上面的"Detail"视图,该视图使用"ServerDetailViewModel"类生成一些数据以传递给视图。

以下是ServerDetailViewModel((代码:

public class ServerDetailViewModel
{
    public RackInfo RackInfo { get; private set; }
    public Server Server { get; private set; }
    public string Name { get; private set; }
    public ServerDetailViewModel(RackInfo rackInfo, Server server)
    {
         Server = server;
         RackInfo = rackInfo;
         *more code here that sets stuff*
    }
}

我认为我的问题与表单参数的传递方式有关。

我可以显式地对一些服务器属性进行编码,然后将它们保存到数据库表中,这似乎很奇怪。但是,当我尝试使用UpdateModel做任何事情时,似乎都不会成功。当我使用UpdateModel时,它会将我重定向到详细信息页面,但我为属性输入的值似乎都没有出现。此外,我通过表单输入的任何属性都不会保存到数据库表中。

如果你们中有人看过Nerd Dinner教程(http://weblogs.asp.net/scottgu/archive/2009/04/28/free-asp-net-mvc-nerddinner-tutorial-now-in-html.aspx),这就是我一直在用的。当我完成了一半时,我决定开始一个新的项目,实现我正在使用的东西。

我最初能够让Create工作,但在向控制器添加另一个单独的存储库后,Create方法中断了。。

如有任何意见或见解,我们将不胜感激。如果您需要任何其他信息,请告诉我。

谢谢S.O。!!

编辑(包括"创建"表单(:

@model PACSSL.Models.ServerFormViewModel
@using (Html.BeginForm())

{@Html。AntiForgeryToken((

<div class="form-horizontal">
    <h4>Server</h4>
    <hr />
    @Html.ValidationSummary(true)
    <div class="form-group">
        @Html.LabelFor(model => model.Server.Name, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Server.Name)
            @Html.ValidationMessageFor(model => model.Server.Name)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Domain, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(x => x.Domain, Model.Domain)
            @Html.ValidationMessageFor(model => model.Domain)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.BackedUp, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(x => x.BackedUp, Model.BackedUp)
            @Html.ValidationMessageFor(model => model.BackedUp)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Server.Role, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Server.Role)
            @Html.ValidationMessageFor(model => model.Server.Role)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Server.GroupOwner, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Server.GroupOwner)
            @Html.ValidationMessageFor(model => model.Server.GroupOwner)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Server.PatchNotes, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Server.PatchNotes)
            @Html.ValidationMessageFor(model => model.Server.PatchNotes)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Server.LastPatched, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Server.LastPatched) Format: yyyy-mm-dd
            @Html.ValidationMessageFor(model => model.Server.LastPatched)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.PatchedBy, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(x => x.PatchedBy, Model.PatchedBy)
            @Html.ValidationMessageFor(model => model.PatchedBy)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.VP, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(x => x.VP, Model.VP)
            @Html.ValidationMessageFor(model => model.VP)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Server.VMHost, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Server.VMHost)
            @Html.ValidationMessageFor(model => model.Server.VMHost)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Server.Location, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Server.Location)
            @Html.ValidationMessageFor(model => model.Server.Location)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Server.PurchaseDate, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Server.PurchaseDate) Format: yyyy-mm-dd
            @Html.ValidationMessageFor(model => model.Server.PurchaseDate)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Server.OS, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Server.OS)
            @Html.ValidationMessageFor(model => model.Server.OS)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Server.Model, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Server.Model)
            @Html.ValidationMessageFor(model => model.Server.Model)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Server.DellST, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Server.DellST)
            @Html.ValidationMessageFor(model => model.Server.DellST)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Server.ServiceContract, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Server.ServiceContract) Format: yyyy-mm-dd
            @Html.ValidationMessageFor(model => model.Server.ServiceContract)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Server.IsLive, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Server.IsLive)
            @Html.ValidationMessageFor(model => model.Server.IsLive)
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>

}

UpdateModel not working (MVC)

这是您的问题。

当您使用表达式model => model.Server.OS生成表单字段时,您会生成一个具有"name"属性的字段,如下所示:

"Server.OS"

然后,当您尝试将其绑定到模型时,它将绑定到与表达式"Server.OS"匹配的模型属性-因此,如果您的模型对象是server,它将与以下对象绑定:

server.Server.OS

我想,当您想绑定到server.OS时。我的建议是使视图模型变平,这样就不会有Model.Server.OS,而只有Model.OS。然后当你这样做的时候…

UpdateModel(server);

它应该将"OS"字段绑定到"OS"属性。

简而言之:与server对象相比,您的视图模型具有额外级别的属性嵌套,因此生成的字段名称和您试图绑定的属性不匹配。

更好的是,通过修改控制器操作以具有签名,绑定回相同的视图模型:

public ActionResult Create(ServerDetailViewModel model)

然后在控制器(或一些静态映射类(中手动进行映射——视图模型和域模型应该是完全独立的。

您正在尝试更新一个"空"模型。它是空的,因为您正在创建一个新实例。

  Server server = new Server();
try
{
    //At this point you have an empty model because you just 
    //created a new Instance of your Server class,
    //It is updating the Model with all Null values because they aren't assigned to anything
    UpdateModel(server);  <----- This is not working

     //Here you assign values to your model, hence why they are not null anymore 
     // Your other values aren't getting saved because they still don't have a value.         
     server.Name = "testAdd";
     server.OS = "2008 R2";
    serverRepository.Add(server);
    serverRepository.Save();
    return RedirectToAction("Details", new { id = server.ServerID });
 }