我想從視圖強制類型傳遞給控制器。我的觀點是強烈的類型。不知何故,當控制器中調用「Save」操作方法時,我在屬性值中獲得「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; }
}
}
是否有產生Submit按鈕任何輔助方法? – Asdfg 2012-07-26 16:38:22
不,但是可能有很多創建自己的幫手來做這件事的例子。 – 2012-07-26 16:39:56