2012-08-06 23 views
2

這其中有我百思不得其解完全...型號是空的HttpPost

我有一個非常簡單的輸入模型,有三點的屬性,像這樣:

public class SystemInputModel 
{ 
    public string Name; 
    public string Performance; 
    public string Description; 
} 

在我的控制器:

public ViewResult AddSystem() 
{ 
    return View(new SystemInputModel()); 
} 


[HttpPost] 
public ActionResult AddSystem(SystemInputModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     var system = new OasisSystem 
     { 
      Name = model.Name, 
      Description = model.Description, 
      Performance = model.Performance 
     }; 
     return Json(repository.AddSystem(system)); 
    } 
    return Json(new {success = false, message = "Internal error"}); 
} 

的視圖:

<h2>Add System</h2> 

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 
    <label>Name</label> 
    @Html.EditorFor(m => m.Name) 
    <br /> 
    <label>Description</label> 
    @Html.EditorFor(m => m.Description) 
    <br /> 
    <label>Performance</label> 
    @Html.EditorFor(m => m.Performance) 
    <br /> 

    <input type="submit" /> 
} 

我填寫表單並點擊提交。我已經看過了原郵政提琴手:

POST http://localhost.:4772/System/AddSystem HTTP/1.1 
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */* 
Referer: http://localhost.:4772/System/AddSystem 
Accept-Language: en-us 
Content-Type: application/x-www-form-urlencoded 
UA-CPU: x86 
Accept-Encoding: gzip, deflate 
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E; MS-RTC LM 8) 
Connection: Keep-Alive 
Content-Length: 42 
Host: localhost.:4772 
Pragma: no-cache 
Cookie: ASP.NET_SessionId=5i4jtrrhjuujvtbpsju4bu3f 

Name=foo&Description=bar&Performance=yes 

然而,正在傳遞到我的HttpPost控制器方法模型對所有三個屬性的空值。如果我將其從模型對象更改爲FormCollection,則可以看到傳入的三個屬性。

爲什麼不將已發佈的字段映射到我的模型對象?

回答

5

默認的MVC 3模型聯編程序需要您的模型的屬性。

如果你改變你的模型如下:

public class SystemInputModel 
{ 
    public string Name { get; set; } 
    public string Performance { get; set; } 
    public string Description { get; set; } 
} 

一切都會好起來。

+0

Doh!我知道這一定很簡單。非常感謝。 – 2012-08-07 01:59:27