我是新來的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>
嘗試更改參數名稱。有時MVC會與這些事情混淆。現在將其命名爲「鞋子」。 – Frayt 2015-04-04 17:31:44
看起來像mvc不能反序列化你的模型。嘗試用FormsCollection表單替換您的模型,看看它是否顯示。 – Aram 2015-04-04 17:34:34
@Frayt我改變了參數的名稱,仍然沒有骰子。 – 2015-04-04 17:52:09