我非常确定我的大脑是朋友,这是我明天早上会笑的事情,但不幸的是我仍然坚持这一部分并且正在寻求帮助。
我有一个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,这是可用的房屋ID。我试过设置一个隐藏的值。我设置了一个断点,我设置了隐藏字段的值,我看着它发生了变化,但它没有进入控制器。所以我将其更改为表单提交,并尝试将其作为路由值捕获并且也无法正常工作。我不知所措,谁能看到我出错的地方?
编辑:添加视图
@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">
}
答案 0 :(得分:0)
路线:
@Html.BeginForm("Move", "Housing", new { personID = @Model.person.ID , housingID= @Model.newHousing.AvailableHousingID}, FormMethod.Post, new { })