我很確定我的大腦是朋友,這是我明天早上要笑的東西,但不幸的是我卡在這一部分,並尋求幫助。無法將價值傳遞給控制器從視圖通過viewmodel
我有一個視圖模型:
public class HousingDetailsViewModel : AppViewModel
{
DataContext db = new DataContext();
public List<string> units { get; set; }
public List<AvailableHousing> availableHousings { get; set; }
public Person person { get; set; }
public OccupiedHousing currentHousing { get; set; }
public OccupiedHousing newHousing;
public HousingDetailsViewModel(int? id)
{
units = db.AvailableHousings.OrderBy(ah => ah.Unit).Select(h => h.Unit).Distinct().ToList();
availableHousings = db.AvailableHousings.Where(h => h.Available == true).OrderBy(h => h.Bed)
.OrderBy(h => h.Room).ToList();
currentHousing = db.OccupiedHousing.Include(o => o.AvailableHousing)
.Include(o => o.Person).Where(o => o.PersonID == id && o.CurrentHousing == true).FirstOrDefault();
person = db.Persons.Find(id);
newHousing = new OccupiedHousing();
}
}
這種觀點我的控制器方法:
public ActionResult Details(int? id)
{
return View(new HousingDetailsViewModel(id));
}
[HttpPost]
public ActionResult Move(OccupiedHousing newHousing, int? personID)
{
newHousing.PersonID = personID;
newHousing.DateArrived = DateTime.Now;
newHousing.CurrentHousing = true;
newHousing.AvailableHousingID = housingID;
db.OccupiedHousings.Add(newHousing);
db.SaveChanges();
return RedirectToAction("Index", new HousingViewModel());
}
而且我的表單工作正常,所有除1我的領域,這就是該AvailableHousingID。我試着設置一個隱藏的值。我在設置隱藏字段的值處放置了一個斷點,並且我觀察它發生了變化,但是它沒有將其傳遞給控制器。所以我將它改爲表單提交,並嘗試將其作爲路由值來捕獲,但這也不起作用。我無所適從,任何人都可以看到我要去哪裏錯了?
編輯:添加視圖
@model AppName.ViewModels.HousingDetailsViewModel
@{
ViewBag.Title = "Housing Details";
}
@Html.BeginForm("Move", "Housing", new { personID = @Model.person.ID }, FormMethod.Post, new { })
<script>
function setID(id) {
$('#HiddenHousingID').val(id);
$('#HiddenSubmit').click();
}
</script>
<h2>Housing Details</h2>
<div class="row">
<div class="col-xs-12 container">
<div class="col-xs-5">
<img src="//placehold.it/150x200/77CCDD/66BBCC" class="img-responsive" />
</div>
<div class="col-xs-7">
<h4>@Model.person.ComboName</h4>
<h4>@Model.currentHousing.AvailableHousing.Unit - @Model.currentHousing.AvailableHousing.Room - @Model.currentHousing.AvailableHousing.Bed</h4>
<h4>@Model.person.DateOfBirth.Value.ToShortDateString()</h4>
@Html.HiddenFor(m => m.newHousing.AvailableHousingID, new { id = "HiddenHousingID", name = "newHousing.AvailableHousingID")}
</div>
</div>
</div>
<div class="row">
@foreach (var unit in Model.units)
{
<div class="col-xs-6">
<div class="panel panel-primary">
<div class="panel-heading">
<span class="panel-title">
@unit
</span>
</div>
<div class="panel-body">
<table id="MoveHousingTable" class="table table table-condensed table-striped">
<thead>
<tr>
<th>
Available Housing
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var housing in Model.availableHousings.Where(h => h.Unit == unit))
{
<tr>
<td>
@housing.Room -
@housing.Bed
</td>
<td>
<input type="button" value="Select" name="select" onclick="setID(@housing.ID)" />
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
}
</div>
<input type="submit" class="hidden" id="HiddenSubmit">
}
可你也同意這樣的觀點 – Usman
(1)查看模型永遠不應該包含數據庫訪問代碼(2)查看模型在編輯時不應包含數據模型。 (3)你將'HousingDetailsViewModel'傳遞給視圖,這樣你的post方法需要是'public ActionResult Move(HousingDetailsViewModel model)'(4)你看起來沒有無參數的構造函數,所以DefaultModelBinder會拋出一個異常你修復點3 –
@Usman,我已更新顯示視圖。我展示了BeginForm的用法,我不使用Move方法的housingID參數。我想我應該把它拿出來。 – Kramb