I am new to the ASP.NET MVC framework and taking a course to get my self up to speed with it. In doing so I am working on a project to cover all the important content and I have now ran into problem :
I have a page set up to simply display some html content to the client and then if a user wishes to participate, they can click a button to show a Form which they can then use to fill in some required data. The following is the javascript used to show the appropriate tag :
<script>
$(document).ready(function(){
$("#btnOneWayTrip").click(function () {
$("#TripFromPlaceHoler").show();
});
$("#TripFromPlaceHoler").hide();
});
and in the 'TripFromPlaceHoler' div I simply Have a form placed inside it, the form's markup looks like this :
@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>
}
My Controller looks like this :
public class TripsController : Controller
{
//
// GET: /Trip/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult CreateOneWayTrip(Models.TestTripClass model)
{
//Do something with model here
return Redirect("~/Trips/Index");
}
}
Here is my Test Model that is referred to a couple of times :
public class TestTripClass
{
public string TripID { get; set; }
public string StartPoint { get; set; }
public string EndPoint { get; set; }
public DateTime StartTime { get; set; }
}
When I fill in the form, and press the submit button, only a blank model returns when I debug and inspect the parameter of my 'CreateOneWayTrip' ActionResult, called 'model' (as seen in the above code). Why is this happening? Feel free to share any suggestions since I am new to MVC.
As a overview, here is the code inside my .cshtml file :
@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>