最初のものが最初です:そのような質問があったことは知っていますが、私はそれらの多くを読んだことがあります。私はビューにモデルを送信する単純なアクションを持っています(すべてのhiddenforフィールドは私が見ているものから正しいです)が、ポストアクションはいくつかのプロパティを取得しません。ここにコードがあります。投稿後にMVC 4.0モデルにヌル値があります
コントローラー:
//
// 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アクションの最初の2行から取得しています:これらのフィールドのすべてが正しいですが、ポストアクションでCOMPUTERパラメータで、(あなたが上記のポストフィールドで見ることができるように)
__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
ルーム場を無効である。何故ですか?