Calidus
Calidus

Reputation: 1414

MVC4 Misunderstanding Models interaction with HTTP Post

I feel like what I am trying to do should be really easy(and it is making me feel dumb), but since I can't figure it out I think I am miss understanding how HTTP POST.

Here is how I think should happen: The controller creates QuoteDimensionViewModel QDVM then sends it to the view. The View creates webpage which contains the textboxes for various variables in the model. The user fills in those textboxes. The user clicks the buttom which generates the HTTP post, then the model is updated with the new values from the textbox and sent to the controller. The controller does it stuff and we all live happily ever after.

Here is what I think is happening: When the user hits the button and a new QuoteDimensionViewModel is being created and only the values with text boxes associated with them are getting filled. So in this case I don't have a textbox for the variable Eid, so the value for Eid could be 5 in the Get but when Post the value is always 0. This causes a problem when trying to Validate the Data with IValidatableObject.

TLDR; I basically want to send a model with X variables to a view, then change Y of those variables. Validate the updated model with the changes and POST it back to the controller.

Thanks for any help and suggestions

First a the model:

public class QuoteDimensionViewModel
    {
        private QuoteDatabaseEntities Quotedb = new QuoteDatabaseEntities();
        [Required]
        public string ShowerImage { get; set; }
        [Required]
        public EnclosureDimension EnclosureDimensions { get; set; } \\Wrapper class
        [Required]
        public QuoteDimensionsWrapper QuoteDimensions { get; set; } \\Wrapper class
        [Required]
        public int Eid { get; set; }
        [Required]
        public int Sid { get; set; }
        [Required]
        public int Hid { get; set; }
        [Required]
        public int Gid { get; set; }
        public QuoteDimensionViewModel(){ }
        public QuoteDimensionViewModel(int eid, int sid, int hid, int gid)
        {
            Eid = eid;
            Sid = sid;
            Hid = hid;
            Gid = gid;


            this.ShowerImage = (from x in Quotedb.Showers
                           where x.ID == Sid
                           select  x.Picture ).Single();

            this.EnclosureDimensions = (from x in Quotedb.EnclosureDimensions


                                          where (x.Enclosure_ID_FK == Eid)
                                       select x).Single();
            this.QuoteDimensions = new QuoteDimensionsWrapper(Eid);





           }
    }
public class QuoteDimensionsWrapper: IValidatableObject
        {
            public int Eid { get; set; }
            public int ID { get; set; }
            public int Quote_ID_FK { get; set; }
            public double Height { get; set; }
            public double Width { get; set; }

            public QuoteDimensionsWrapper(int eid)
            {
                Eid = eid;
            }
            public QuoteDimensionsWrapper()
            {

                }
            public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
            {
             \\Validation stuff based on the value of Eid
             }

Now the Controller:

 public class QuoteDimensionViewModelController : Controller
{

    public ActionResult Quote(int Eid, int Sid, int Hid, int Gid)
    {

        QuoteDimensionViewModel QDVM = new QuoteDimensionViewModel(Eid, Sid, Hid, Gid);

        return View(QDVM);
    }
    [InitializeSimpleMembership]
    [HttpPost]
    public ActionResult Quote(QuoteDimensionViewModel QDVM)
    {
         //Validate the Data then write it to a database

        return RedirectToAction("Index", "QuoteViewModel", new { id = myQid });

    }

}

}

and the View:

@model QuoteWebApp.Models.ViewModels.QuoteDimensionViewModel

@{
    ViewBag.Title = "Quote";
}

<h2>Dimensions</h2>

<img [email protected](Model.ShowerImage) alt="Image" />

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>QuoteDimensions</legend>

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

     }
        <p>
            <input type="submit" value="Quote"  />
        </p>
    </fieldset>
}
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Upvotes: 1

Views: 139

Answers (1)

BlackICE
BlackICE

Reputation: 8926

Create hidden fields for any values you want to persist from the GET to the POST but don't want the user to see.

@Html.HiddenFor(model=>model.Eid)

Upvotes: 1

Related Questions