2017-07-04 63 views
1

我的ViewModel總是返回null,不知道爲什麼。有人可以看看我的代碼,並檢查這裏有什麼不對,以及爲什麼我的填充模型與視圖中的數據返回到控制器爲null發佈ViewModel總是NULL

public class PaintballWorkerCreateViewModel 
{ 
    public PaintballWorker PaintballWorker { get; set; } 
    public PaintballWorkerHourlyRate HourlyRate { get; set; } 
} 

控制器

public ActionResult Create() 
{ 
    PaintballWorkerCreateViewModel model = new PaintballWorkerCreateViewModel() 
    { 
     PaintballWorker = new PaintballWorker(), 
     HourlyRate = new PaintballWorkerHourlyRate() 
     { 
      Date = DateTime.Now 
     } 
    }; 
    return View(model); 
} 

[HttpPost] 
[PreventSpam(DelayRequest = 20)] 
[ValidateAntiForgeryToken] 
public ActionResult Create(PaintballWorkerCreateViewModel paintballWorker) 
{ 
    (...) 
} 

視圖,甚至加入HiddenFor的ID(未在控制器GET函數創建)。

@model WerehouseProject.ViewModels.PaintballWorkerCreateViewModel 

@{ 
    ViewBag.Title = "Utwórz pracownika"; 
    Layout = "~/Views/Shared/_Layout_Paintball.cshtml"; 
} 

<h2>Dodawanie pracownika</h2> 

@using (Html.BeginForm("Create", "PaintballWorkers", FormMethod.Post)) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     @Html.HiddenFor(model => model.PaintballWorker.Active) 
     @Html.HiddenFor(model => model.PaintballWorker.MoneyGot) 
     @Html.HiddenFor(model => model.PaintballWorker.PaintballWorkerID) 
     @Html.HiddenFor(model => model.HourlyRate.Date) 
     @Html.HiddenFor(model => model.HourlyRate.PaintballWorkerID) 
     @Html.HiddenFor(model => model.HourlyRate.PWHourlyRateID) 

     <div class="form-group"> 
      @Html.LabelFor(model => model.PaintballWorker.Imie, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.PaintballWorker.Imie, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.PaintballWorker.Imie, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.PaintballWorker.Nazwisko, htmlAttributes: new { @class = "control-label col-md-2" }) 

(...) 

     <div class="form-group"> 
      @Html.LabelFor(model => model.HourlyRate.HourlyRate, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.HourlyRate.HourlyRate, new { htmlAttributes = new { @class = "form-control", @type = "number", @min = "0.1", @step = "0.1", @value = "10" } }) 
       @Html.ValidationMessageFor(model => model.HourlyRate.HourlyRate, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Dodaj pracownika" class="btn btn-primary" /> 
      </div> 
     </div> 
    </div> 
} 

<div> 
@Html.ActionLink("Powrót do listy", "Index", new object { }, new { @class = "btn btn-default" }) 
</div> 
+0

是不是所有值都爲空或只有幾個值? –

+0

'PaintballWorker'和'HourlyRate'爲空,即使我創建了模型並在GET函數中填充了數據。 – Cezar

+0

你可以顯示2個類的代碼嗎? –

回答

0

您的代碼看起來不錯..看起來像它的參考正在某處失去了..你試圖刪除[PreventSpam(DelayRequest = 20)]屬性?所以你的控制器會是這樣的:

public ActionResult Create() 
    { 
      PaintballWorkerCreateViewModel model = new PaintballWorkerCreateViewModel() 
      { 
       PaintballWorker = new PaintballWorker(), 
       HourlyRate = new PaintballWorkerHourlyRate() 
       { 
        Date = DateTime.Now 
       } 
      }; 
      return View(model); 
    } 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(PaintballWorkerCreateViewModel paintballWorker) 
    { 
     (...) 
    } 
+0

不,刪除'PreventSpam'沒有幫助 – Cezar

0

您不是正確發佈表單。由於屬性是隱藏的,默認情況下它們是空的。

發佈後,您的控制器沒有看到任何東西,因爲元素被隱藏。因此它是空的。

改爲使用擴展方法@ Html.TextboxFor。 Mvc viewengine將呈現文本框,然後您可以放置​​一些值併發布它們。

您還需要確保在代碼中正確映射了路由。

+0

不,這不是問題,我已將'HiddenFor'更改爲'EditorFor',仍然都是'null' – Cezar

0

您的問題可能是因爲命名衝突,那是後動作的參數名稱可能不一樣的屬性名稱在您的視圖模型

請按照下面的鏈接:

https://forums.asp.net/t/1670962.aspx?ViewModel+in+post+action+is+null

因爲它清楚地指出了問題的解決方案和路線原因。

注:我也有同樣的問題,回來了,並按照上述相同的方式解決它。希望它能對您有用太

感謝

KARTHIK