C# UpdateModel不工作(MVC)
我一直在学习书呆子晚餐教程。我分道扬镳,在它的基础上创建了自己的项目,但我很难让UpdateModel正常工作。这看起来很复杂,所以我将在下面详细解释 在ServersController.cs中:C# UpdateModel不工作(MVC),c#,forms,asp.net-mvc-4,C#,Forms,Asp.net Mvc 4,我一直在学习书呆子晚餐教程。我分道扬镳,在它的基础上创建了自己的项目,但我很难让UpdateModel正常工作。这看起来很复杂,所以我将在下面详细解释 在ServersController.cs中: public ActionResult Create(FormCollection formValues) { Server server = new Server(); try { UpdateModel(server); <----- This
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**
}
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()代码:
我认为我的问题与表单参数的传递方式有关
奇怪的是,我可以显式地编写一些服务器属性的代码,然后将它们保存到数据库表中。但是当我尝试使用UpdateModel做任何事情时,似乎什么都没有发生。当我使用UpdateModel时,它会将我重定向到详细信息页面,但是我为属性输入的值似乎都不会出现。此外,我通过表单输入的所有属性都不会保存到数据库表中
如果你们中的任何一个人已经完成了书呆子晚餐教程(),那就是我一直在使用的。当我完成了一半的时候,我决定开始一个新的项目来实现我正在使用的东西
我最初能够让Create工作,但在向控制器添加另一个单独的存储库后,Create方法中断了
如果您对此有任何意见或见解,我们将不胜感激。如果您需要任何其他信息,请告诉我
谢谢S.O
编辑(包括“创建”表单):
{
@Html.AntiForgeryToken()
服务器
@Html.ValidationSummary(true)
@LabelFor(model=>model.Server.Name,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Server.Name)
@Html.ValidationMessageFor(model=>model.Server.Name)
@LabelFor(model=>model.Domain,新的{@class=“controllabel col-md-2”})
@DropDownListFor(x=>x.Domain,Model.Domain)
@Html.ValidationMessageFor(model=>model.Domain)
@LabelFor(model=>model.BackedUp,新的{@class=“controllabel col-md-2”})
@DropDownListFor(x=>x.BackedUp,Model.BackedUp)
@Html.ValidationMessageFor(model=>model.BackedUp)
@LabelFor(model=>model.Server.Role,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Server.Role)
@Html.ValidationMessageFor(model=>model.Server.Role)
@LabelFor(model=>model.Server.GroupOwner,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Server.GroupOwner)
@Html.ValidationMessageFor(model=>model.Server.GroupOwner)
@LabelFor(model=>model.Server.PatchNotes,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Server.PatchNotes)
@Html.ValidationMessageFor(model=>model.Server.PatchNotes)
@LabelFor(model=>model.Server.LastPatched,新的{@class=“controllabel col-md-2”})
@Html.EditorFor(model=>model.Server.LastPatched)格式:yyyy-mm-dd
@Html.ValidationMessageFor(model=>model.Server.LastPatched)
@LabelFor(model=>model.PatchedBy,新的{@class=“controllabel col-md-2”})
@DropDownListFor(x=>x.PatchedBy,Model.PatchedBy)
@Html.ValidationMessageFor(model=>model.PatchedBy)
@LabelFor(model=>model.VP,新的{@class=“controllabel col-md-2”})
@DropDownListFor(x=>x.VP,Model.VP)
@Html.ValidationMessageFor(model=>model.VP)
@LabelFor(model=>model.Server.VMHost,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Server.VMHost)
@Html.ValidationMessageFor(model=>model.Server.VMHost)
@LabelFor(model=>model.Server.Location,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Server.Location)
@Html.ValidationMessageFor(model=>model.Server.Location)
@LabelFor(model=>model.Server.PurchaseDate,新的{@class=“controllabel col-md-2”})
@Html.EditorFor(model=>model.Server.PurchaseDate)格式:yyyy-mm-dd
@Html.ValidationMessageFor(model=>model.Server.PurchaseDate)
@LabelFor(model=>model.Server.OS,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Server.OS)
@Html.ValidationMessageFor(model=>model.Server.OS)
@LabelFor(model=>model.Server.model,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Server.model)
@Html.ValidationMessageFor(model=>model.Server.model)
@LabelFor(model=>model.Server.DellST,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Server.DellST)
@Html.ValidationMessageFor(model=>model.Server.DellST)
@LabelFor(model=>model.Server.ServiceContract,新的{@class=“controllabel col-md-2”})
@Html.EditorFor(model=>model.Server.ServiceContract)格式:yyyy-mm-dd
@Html.ValidationMessageFor(model=>model.Server.ServiceContract)
@LabelFor(model=>model.Server.IsLive,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Server.IsLive)
@Html.ValidationMessageFor(mod
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*
}
}
@model PACSSL.Models.ServerFormViewModel
@using (Html.BeginForm())
<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>
"Server.OS"
server.Server.OS
UpdateModel(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 });
}