views:

10

answers:

1

I have a PartialView strongly typed to a view model

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<VATRateManager_MVC.Models.ViewModels.RateControlEditViewModel>" %>

<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>

<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm("Edit", "Rate", FormMethod.Post))
{ %>
<%=Html.ValidationSummary() %>

<%=Html.HiddenFor(Model => Model.ParentID)%>
<%=Html.HiddenFor(Model => Model.ParentTypeID)%>
<%=Html.HiddenFor(Model => Model.ParentTypeDescription)%>
<%=Html.HiddenFor(Model => Model.RateID)%>

<div >
    <%=Html.LabelFor(Model => Model.RateValue)%>
    <%=Html.DisplayFor(Model => Model.RateValue) %>
</div>

<div>
    <%=Html.LabelFor(Model => Model.StartDate)%>
    <%=Html.DisplayFor(Model => Model.StartDate, new { @class = "date" })%>
   <%-- <%=Html.EditorFor(Model => Model.StartDate, new { @class = "date" })%>--%>
</div>

<div>
    <input type="submit" name="button" value="Edit"/>
</div>

Heres the viewmodel

public class RateControlEditViewModel
{
    /// <summary>The parent ID is the Rate ID for the Current record. </summary>
    [HiddenInput(DisplayValue = false)]
    public int ParentID { get; set; }

    /// <summary>The parent Type ID is the Type ID for the Current record. </summary>
    [HiddenInput (DisplayValue= false)]
    public int ParentTypeID { get; set; }

    [HiddenInput(DisplayValue = false)]
    public string ParentTypeDescription { get; set; }

    /// <summary>Rate ID for current record, may be null if adding new rate. </summary>
    [HiddenInput(DisplayValue = false)]
    public int? RateID { get; set; }

    /// <summary>Percentage VAT Rate to 2 decimal places. </summary>
    [DisplayName("VAT Rate (%)")]
    [Required(ErrorMessage ="Please Supply VAT Rate as percentage value")]
    [Range(typeof(Decimal), "0","100")]
    public decimal RateValue { get; set; }

    /// <summary>Start date from which this VAT Rate will be active, the start date will also be
    /// the same value for the previous records End Date to ensure there are no gaps in the Rate.</summary>
    [DisplayName("VAT Rate Start Date")]
    [DataType(DataType.Date)]
    [Required(ErrorMessage="Please Supply Start date from which this VAT rate will become active")]
    public DateTime? StartDate { get; set; }

}

And heres the Controller method...

  [ActionName("Edit")]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(string button, RateControlEditViewModel model)
    { // Do Stuff here 
    }

My problem is the object "RateControlEditViewModel model" passed into the controller has lost some of its values... Basically screen one should show a list of objects, when you click "Edit" a new view should open displaying the 1 object you clicked on to allow you to edit it - but its losing the values. Help!

A: 

Ok, It seems that if you are using "DisplayFor", the model attributes aren't populated. Using my example below, the values I receive back to the controller are those in the "HiddenFor" objects, but not those in the "DisplayFor".

<%=Html.HiddenFor(Model => Model.ParentID)%>
<%=Html.HiddenFor(Model => Model.ParentTypeID)%>
<%=Html.HiddenFor(Model => Model.ParentTypeDescription)%>
<%=Html.HiddenFor(Model => Model.RateID)%>
<div >
    <%=Html.LabelFor(Model => Model.RateValue)%>
    <%=Html.DisplayFor(Model => Model.RateValue) %>
</div>

<div>
    <%=Html.LabelFor(Model => Model.StartDate)%>
    <%=Html.DisplayFor(Model => Model.StartDate, new { @class = "date" })%>
</div>

To resolve this I just included "HiddenFor"s for the other 2 attributes :)

<%=Html.HiddenFor(Model => Model.RateValue)%>
<%=Html.HiddenFor(Model => Model.StartDate)%>

Simple I think - hope it helps someone else.

TChamberlainGE

related questions