2015-04-04 24 views
0

我是新來的ASP.NET MVC框架,並採取一個課程,讓我自己瞭解它的速度。在這樣做時,我正在研究一個覆蓋所有重要內容的項目,並且現在我遇到了問題:模型數據沒有通過表單傳播到控制器ASP.NET MVC4

我有一個頁面設置爲僅向客戶端顯示一些html內容,然後如果用戶希望參與,他們可以點擊一個按鈕來顯示一個表格,然後他們可以使用它來填寫一些所需的數據。以下是用於顯示相應的標記的JavaScript:

<script> 
$(document).ready(function(){ 
$("#btnOneWayTrip").click(function() { 
    $("#TripFromPlaceHoler").show(); 
}); 

$("#TripFromPlaceHoler").hide(); 
}); 

,並在「TripFromPlaceHoler」 DIV我根本就放在裏面的表單,表單的標記看起來是這樣的:

@using (Html.BeginForm("CreateOneWayTrip","Trips")) { 
@Html.ValidationSummary(true) 

<fieldset> 
    <legend>TestTripClass</legend> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.TripID) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.TripID) 
     @Html.ValidationMessageFor(model => model.TripID) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.StartPoint) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.StartPoint) 
     @Html.ValidationMessageFor(model => model.StartPoint) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.EndPoint) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.EndPoint) 
     @Html.ValidationMessageFor(model => model.EndPoint) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.StartTime) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.StartTime) 
     @Html.ValidationMessageFor(model => model.StartTime) 
    </div> 

    <p> 
     <input type="submit" value="Create" /> 
    </p> 
</fieldset> 
} 

我控制器看起來是這樣的:

​​

這裏是被稱爲幾次我的測試模型:

public class TestTripClass 
{ 
    public string TripID { get; set; } 
    public string StartPoint { get; set; } 
    public string EndPoint { get; set; } 
    public DateTime StartTime { get; set; } 

} 

當我填寫表單並按下提交按鈕時,當我調試並檢查'CreateOneWayTrip'ActionResult的參數(稱爲'model'(如上面的代碼所示))時,只有空白模型返回。這是爲什麼發生?自從我是MVC的新手之後,請隨時分享任何建議。

作爲一個概述,這裏是我的.cshtml文件中的代碼:

@model TransNex.Models.TestTripClass 
@{ 
    ViewBag.Title = "Index"; 
} 

<div class="row"> 
    <div class="col-lg-6" id="TripFromPlaceHoler"> 

     @using (Html.BeginForm("CreateOneWayTrip", "Trips")) 
     { 

      <fieldset> 
       <legend>Create One Way Trip</legend> 

       <div class="editor-label"> 
        @Html.LabelFor(model => model.TripID) 
       </div> 
       <div class="editor-field"> 
        @Html.EditorFor(model => model.TripID) 
       </div> 

       <div class="editor-label"> 
        @Html.LabelFor(model => model.StartPoint) 
       </div> 
       <div class="editor-field"> 
        @Html.EditorFor(model => model.StartPoint) 
       </div> 

       <div class="editor-label"> 
        @Html.LabelFor(model => model.EndPoint) 
       </div> 
       <div class="editor-field"> 
        @Html.EditorFor(model => model.EndPoint) 
       </div> 

       <div class="editor-label"> 
        @Html.LabelFor(model => model.StartTime) 
       </div> 
       <div class="editor-field"> 
        @Html.EditorFor(model => model.StartTime) 
       </div> 

       <p> 
        <input type="submit" value="Create" /> 
       </p> 
      </fieldset> 
     } 
    </div> 
</div> 

<script> 
$(document).ready(function(){ 
    $("#btnOneWayTrip").click(function() { 
     $("#TripFromPlaceHoler").show(); 
    }); 

    $("#TripFromPlaceHoler").hide(); 
}); 

</script> 
+0

嘗試更改參數名稱。有時MVC會與這些事情混淆。現在將其命名爲「鞋子」。 – Frayt 2015-04-04 17:31:44

+0

看起來像mvc不能反序列化你的模型。嘗試用FormsCollection表單替換您的模型,看看它是否顯示。 – Aram 2015-04-04 17:34:34

+0

@Frayt我改變了參數的名稱,仍然沒有骰子。 – 2015-04-04 17:52:09

回答

0

我想通了。前陣子一個由自己編輯模板字符串值定義(由「共享」文件夾下創建「EditorTemplates」文件夾中的模板被稱爲String.cshml並且含有以下代碼:

<input type="text" class="form-control">

當你調用Html.EditorFor助手,默認情況下會創建一個與所有相關的標記如下所示標籤:

<input data-val="true" id="StartPoint" name="StartPoint" type="text" value="" />

但是,由於覆蓋默認字符串編輯器,標記不再正確呈現。

'name'屬性幫助MVC將控件值映射到相關的類屬性,所以當我定義我自己的模板時,我從來沒有給MVC的方法來知道哪些值屬於哪個模型屬性。

0

ASP.NET MVC模型綁定是不是相當不錯,在反序列化複雜類型。 你可以用不同的方式做到這一點: 一種方法是在窗體上使用模型中的mising屬性名稱來隱藏一個元素。 另一種方式是採取提供相關的複雜的模型綁定的方法之一,這種看看這些網頁的複雜模型綁定: http://erraticdev.blogspot.ca/2010/12/sending-complex-json-objects-to-aspnet.html How to post an array of complex objects with JSON, jQuery to ASP.NET MVC Controller?

0

的問題是從創建的局部視圖視圖。如果你直接將你的htmls移動到根頁面上(沒有部分),提交按鈕將填充你的模型類。

相關問題