首先要做的事情:我知道有这样的问题,我已经阅读了很多,但没有一个有帮助。我有一个简单的动作发送模型到视图(所有隐藏的字段是从我看到的正确)但post post动作没有获得一些属性。这是代码。
控制器:
//
// GET:
public ActionResult EditComputer(int id)
{
IDataProvider dataProvider = new DataProvider();
var computer = dataProvider.GetComputer(id);
return View(computer);
}
//
// POST:
[HttpPost]
public ActionResult EditComputer(COMPUTER computer)
{
Request.InputStream.Position = 0;
var result = new System.IO.StreamReader(Request.InputStream).ReadToEnd();
try
{
IDataProvider dataProvider = new DataProvider();
dataProvider.UpdateComputer(computer);
return RedirectToAction("EditRoom/" + computer.Room.RoomID);
}
catch
{
return View(computer);
}
}
电脑型号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
namespace WebClientDLL
{
public class COMPUTER
{
int _computerID;
IPAddress _addressIP;
PhysicalAddress _addressMAC;
String _namePC;
ROOM _room;
public int ComputerID
{
get { return _computerID; }
set { _computerID = value; }
}
public IPAddress AddressIP
{
get { return _addressIP; }
set { _addressIP = value; }
}
public PhysicalAddress AddressMAC
{
get { return _addressMAC; }
set { _addressMAC = value; }
}
public String NamePC
{
get { return _namePC; }
set { _namePC = value; }
}
public ROOM Room
{
get { return _room; }
set { _room = value; }
}
}
}
房间型号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebClientDLL
{
public class ROOM
{
int _roomID;
String _roomName;
String _building;
public int RoomID
{
get { return _roomID; }
set { _roomID = value; }
}
public String RoomName
{
get { return _roomName; }
set { _roomName = value; }
}
public String Building
{
get { return _building; }
set { _building = value; }
}
}
}
查看:
@model WebClientDLL.COMPUTER
@{
ViewBag.Title = "Edycja komputera";
}
<h2>@ViewBag.Title</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Komputer</legend>
@Html.HiddenFor(model => model.ComputerID)
@Html.HiddenFor(model => model.Room)
@Html.HiddenFor(model => model.Room.RoomID)
@Html.HiddenFor(model => model.Room.RoomName)
@Html.HiddenFor(model => model.Room.Building)
<div class="editor-label">
@Html.LabelFor(model => model.NamePC)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NamePC)
@Html.ValidationMessageFor(model => model.NamePC)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AddressIP)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.AddressIP)
@Html.ValidationMessageFor(model => model.AddressIP)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AddressMAC)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.AddressMAC)
@Html.ValidationMessageFor(model => model.AddressMAC)
</div>
<p>
<input type="submit" value="Zapisz" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Powrót do sali", "EditRoom/" + @Model.Room.RoomID)
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我从EditComputer POST操作的前两行获得:
__RequestVerificationToken=pZR5DUClnJ...
&ComputerID=4
&Room=WebClientDLL.ROOM
&Room.RoomID=1
&Room.RoomName=L2.200
&Room.Building=C-x
&NamePC=PECETx
&AddressIP=192.168.0.24
&AddressMAC=1122334456
虽然所有这些字段都是正确的(正如您在上面的帖子字段中看到的),但是post post中COMPUTER参数中的Room字段为null。那是为什么?
答案 0 :(得分:0)
好的,所以几天后我设法找出了什么是错的。显然它是@Html.HiddenFor(model =&gt; model.Room) - mvc认为它是一个字符串,因为我还没有做任何验证,它没有告诉我任何事情。解决方案是实现接口IModelBinder:
public class COMPUTER : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
return new COMPUTER();
}
}
并将其放入Global.asax:
ModelBinders.Binders.Add(typeof(COMPUTER), new UserModelBinder());