我想从视图强制类型传递给控制器。我的观点是强烈的类型。不知何故,当控制器中调用“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