モデルをビューからコントローラーに強く型付けしようとしています。私の見解は強く型付けされています。コントローラーで「保存」アクションメソッドが呼び出されると、どういうわけかプロパティ値に「null」が表示されます。を使用してAsp.Net MVC 3
います。
これは私のビューがどのように見えるかです:
@model MvcApplication2.Models.Event
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>AddNew</title>
</head>
<body>
@using(Html.BeginForm("Save","Event",FormMethod.Post, Model))
{
<div>
<p>@Html.LabelFor(m=>m.EventName) @Html.TextBoxFor(m=>m.EventName)</p>
<p>@Html.LabelFor(m=>m.Venue) @Html.TextBoxFor(m=>m.Venue)</p>
<p>@Html.LabelFor(m=>m.StartTime) @Html.TextBoxFor(m=>m.StartTime)</p>
<p>@Html.LabelFor(m=>m.EndTime) @Html.TextBoxFor(m=>m.EndTime)</p>
@Html.ActionLink("Save Event", "Save")
</div>
}
</body>
</html>
これは私EventController
のように見える方法です:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;
namespace MvcApplication2.Controllers
{
public class EventController : Controller
{
public string Save(Event eventModel)
{
//Here eventModel.EventName and rest of the properties are null.
return "Saved";
}
}
}
モデルは次のようになります。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication2.Models
{
public class Event
{
public string EventName { get; set; }
public string Venue { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
}
}