私は MVC を初めて使用し、この問題に数日間苦労しています。
フォームをサーバーにポストするとき、値は常に null です。コレクション/リストを使用してモデル自体を使用しようとしましたが、最後に試したアプローチはViewModelを使用することでした。
私が達成しようとしている目標は、ユーザーがサインアップしているイベントへの出席をマークすることです。正しい出席情報を取得してビューに送信しています。チェック ボックスをオンにして、ブール値の Attend.Attended を更新します。デバッグ中、Post アクションの先頭にブレーク ポイントを置きます。モデル、コレクション/リスト、ViewModel は常に null です。
モデル:
public class Attend
{
[Key]
public int AttendID { get; set; }
public virtual UserProfile User { get; set; }
public virtual Event Event { get; set; }
public Boolean SignedUp { get; set; }
public Boolean Attended {get; set; }
}
public class Event
{
[Key]
public long EventID { get; set; }
[Required]
[DisplayName("When is this event?")]
public DateTime DateScheduled { get; set; }
public DateTime DateCreated { get; set; }
[DisplayName("Event Category")]
public String Category { get; set; }
[Required]
[DisplayName("Location")]
public String Location { get; set; }
public string Comments { get; set; }
[Required]
[DisplayName("Event Name")]
public string EventName { get; set; }
[Required]
[DisplayName("Event Description")]
public string EventDescription { get; set; }
public virtual ICollection<Attend> Attends { get; set; }
}
コントローラ:
//
// GET: /Event/Attendance
[HttpGet]
public ActionResult Attendance(long id)
{
try
{
var model = new AttendanceViewModel();
if (db == null)
return HttpNotFound();
if (Request.UrlReferrer != null && Request.UrlReferrer.AbsoluteUri != null)
ViewBag.ReferrerUrl = Request.UrlReferrer.AbsoluteUri;
else
ViewBag.ReferrerUrl = Url.Action("Index");
model.Attending = db.Attends.ToList();
ViewBag.myID = id;
return View(model);
}
catch (Exception ex)
{
Log.Error(ex.Message, ex);
return HttpNotFound();
}
}
//
// POST: /Event/Attendance
[HttpPost]
public ActionResult Attendance(AttendanceViewModel Attending, long id)
{
//POST ACTION...
}
意見:
model CottagesOfHope.ViewModels.AttendanceViewModel
@{
ViewBag.Title = "Attendance";
}
<h2>Mark Attendance</h2>
@using (Html.BeginForm())
{
<fieldset>
<legend>Attendance</legend>
<table>
<thead>
<tr>
<th>Name</th>
<th>Attendance</th>
</tr>
</thead>
<tbody>
@foreach (var j in Model.Attending)
{
if (j.Event.EventID == ViewBag.myId)
{
<tr>
<td>@j.User.FirstName @j.User.LastName</td>
<td>@Html.EditorFor(model => j.Attended)</td>
</tr>
}
}
</tbody>
</table>
<p>
<input type="submit" value="Submit" />
</p>
</fieldset>
}
ビューモデル:
public class AttendanceViewModel
{
public virtual List<Attend> Attending { get; set; }
}
前に言ったように、これがデータを正しくバインドしようとした最後のアプローチでした。どんな助けでも大歓迎です。
前もって感謝します!