【发布时间】:2016-04-16 15:42:35
【问题描述】:
第一件事:我知道有类似的问题,我已经阅读了很多,但没有一个有帮助。我有一个简单的动作发送模型到视图(从我看到的所有 hiddenfor 字段都是正确的)但是 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 字段为空。这是为什么呢?
【问题讨论】:
标签: asp.net-mvc null http-post