0

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>
mason
  • 31,774
  • 10
  • 77
  • 121
Marnus Steyn
  • 1,053
  • 2
  • 16
  • 44
  • Try changing the parameter name. MVC gets confused with these things sometimes. Name it "shoes" for now. – Frayt Apr 04 '15 at 17:31
  • Looks like mvc cannot deserialize your model. Try replace your model in the action with FormsCollection form and see if it shows up. – Aram Apr 04 '15 at 17:34
  • @Frayt I changed the name of the parameter, still no dice. – Marnus Steyn Apr 04 '15 at 17:52
  • @Aram Using the FormCollection I have to know the name of the controls , what are the names of the controls that the Html.EditorFor Generates? – Marnus Steyn Apr 04 '15 at 17:53
  • I updated the original post to include my class, I believe that my problem might be because I have a property in my class that is a List that my form does not provide for and thus mvc cannot deserialze my model – Marnus Steyn Apr 04 '15 at 17:55
  • 1
    Can you post the html of the web page it generates? – wonderbell Apr 04 '15 at 18:27
  • Yes forms collection will need a Key for the element to get the value, i just mentioned that so that you can check and see if anything is being passed in but FormsCollection will not be a good solution after all. – Aram Apr 04 '15 at 19:41
  • Check the network request header to see that the form is posted with names you are expecting in your action/model. This will help narrow down where the problem. – Jasen Apr 04 '15 at 19:56
  • Try to remove form and submit button from partial view, and put these into main view. framework get confused while updating model from from formvaluecollections. – Anupam Singh Apr 04 '15 at 20:49
  • Have you tried @Html.Partial("_CreateOneWayTrip", new TestTripClass());. Also, it would be helpful if you could post the index razor code that actually renders the partial. What is its model? – fordareh Apr 04 '15 at 20:56
  • What is the model defined in the main view? The code you have shown works fine assuming its also `@model Models.TestTripClass` –  Apr 06 '15 at 03:06
  • Ok, so I removed my form from the partial view, and placed it in the body of the page just as it is. I also temporary removed the List<> property from my class. Still the model is coming trough blank. – Marnus Steyn Apr 06 '15 at 19:42

3 Answers3

0

ASP.NET MVC model binder is not very well in deserializing complex types. You could do this in different ways: one way is to have a hidden element on the form with the name of the mising attribute in the model. the other way is to take one of the approaches available related to complex model bindings, for this take a look at these pages for complex model binding: 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?

Community
  • 1
  • 1
Aram
  • 5,537
  • 2
  • 30
  • 41
0

The problem is creating view from a Partial View. If you move your htmls on directly root page(without partial), submit button will fill your model class.

Oğuzhan Soykan
  • 2,522
  • 2
  • 18
  • 33
0

I figured it out. A while back a defined by own Editor Template for string values(by creating the 'EditorTemplates' folder under the 'Shared' folder. The template was called String.cshml and contained the following code :

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

When you call the Html.EditorFor helper, by default it creates a tag with all the relevant markup shown here :

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

However since Override the default string editor, the tag is no longer rendered correctly.

The 'name' property helps MVC to map control values to relevant class properties, so when I defined my own templates, I never gave a means for MVC to know which values belong to which model properties.

Marnus Steyn
  • 1,053
  • 2
  • 16
  • 44