2016-11-13 79 views
0

我試圖按照最佳做法將數據驗證添加到我的用戶界面。我想將數據驗證添加到ViewModel,然後如果該數據有效,請將表單提交給控制器。但是,控制器收到的數據始終爲空值。任何想法我做錯了什麼?這整個MVVC架構讓我感到困惑。我在使用模型提交表單時工作正常,但我無法在模型上獲取數據驗證?傳遞給我的控制器時,ViewModel數據全爲空?

控制器:

[HttpPost] 
[ValidateAntiForgeryToken] 
public async Task<ActionResult> CreateResource(AddResourceViewModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       await (//does something); 
      } 
      return View(); 
     } 

模型視圖:

public class AddResourceViewModel 
{ 

     public string User { get; set; }  
     public string ResourceName { get; set; } 
     public int ResourceId { get; set; } 
     public string Model { get; set; } 
     public float Latitude { get; set; } 
     public float Longitude { get; set; } 
     public decimal Amount { get; set; } 
     public int UnitId { get; set; } 
     public int PrimaryEsfId { get; set; } 
     public IEnumerable<string> Capabilities { get; set; } 
     public IEnumerable<int> AdditionalEsfs { get; set; } 

     public Resource Resource { get; set; } 


} 

CSHTML形式的開頭:

@model erms.ViewModel.AddResourceViewModel 
<form asp-controller="AddResource" asp-action="NewResource" method="post" class="form-horizontal"> 
<div class="panel panel-default"> 
    <div class="panel-body"> 
     <form asp-controller="AddResource" asp-action="CreateResource" method="post" class="form-horizontal"> 

      <div asp-validation-summary="All" class="text-danger"></div> 
      <div class="form-group"> 
       <label asp-for="ResourceId" class="col-md-2 control-label">Resource ID:</label> 
       <div class="col-md-10"> 
        <input asp-for="ResourceId" class="form-control" value="@Model.Resource.ResourceId" readonly /> 
        <span asp-validation-for="ResourceId" class="text-danger"></span> 
       </div> 
      </div> 
+0

你有沒有用'HttpPost'屬性裝飾你的控制器動作?您是否將表單發回服務器或嚴格通過GET請求來完成此操作? –

+0

是的,對不起,我把它從這個例子中排除了。我編輯了原文。 – vercingortix

回答

1

這種東西讓我很沮喪,當學習新的架構,但我已經明白了。這是一個命名約定問題。我已經討論了這個問題,它現在能正常使用:從我的ViewModel

public string Model { get; set; } 

和:從我的控制器

public async Task<IActionResult> NewResource(AddResourceViewModel model) 

有衝突的名字是。所以模式模式 ...

衝突的根據:http://ideasof.andersaberg.com/development/aspnet-mvc-4-model-binding-null-on-post

不要將其命名的動作傳入變量在模型中做張貼相同。這將弄亂模型活頁夾。

0

也許我想這個問題就你不使用剃刀是提交表格。這是我想到的:

@model <AddResourceViewModel> 

@using(Html.BeginForm()) { 

<div asp-validation-summary="All" class="text-danger"></div> 
     <div class="form-group"> 
      <label asp-for="ResourceId" class="col-md-2 control-label">Resource ID:</label> 
      <div class="col-md-10"> 
       @Html.TextboxFor(x => x.ResourceId) 
       @Html.ValidationMessageFor(x => x.ResourceId) 
      </div> 
     </div> 
} 

由於這是我通常用來驗證我的表單,也許這是值得考慮的。但我可能是錯的。

+0

我在標籤幫助程序的cshtml上添加了表單控制器提交。我應該提到這是使用.NET Core,所以TagHelpers取代了Html Helpers: https://dannyvanderkraan.wordpress.com/2016/04/19/asp-net-core-1-0-goodbye-html -helpers-and-hello-taghelpers/ – vercingortix

2

取代return View();return View(model);

+0

我也這樣做了,這有幫助,但它仍然是一個命名約定問題。謝謝。 – vercingortix

相關問題