1

Something weird is happening and I am not able to understand why.. here's the scenario -

I have a model with few properties when I populate the model the properties in model does have values set (checked by putting breakpoints). It comes on the view also but it is not being shown on textbox. It is showing the default value (guessing by seeing the item textbox on the page as it has 0).

Below is my model -

public class PriceEnquiryModel
{
    [DisplayName("Item")]
    public int item { get; set; }

    [DisplayName("Description")]
    public string description { get; set; }

    [DisplayName("UOP")]
    public string uop { get; set; }

    [DisplayName("UOS")]
    public string uos { get; set; }

    [DisplayName("Pack Description")]
    public string pack_description { get; set; }

    [DisplayName("Pack Size")]
    public string PackSize { get; set; }
} 

This is the controller;s code -

public ActionResult Search(PriceEnquiryModel price)
{
    var priceEnquiryModel = new PriceEnquiryModel();

    // Read parameter values from form.
    int item = Convert.ToInt32(Request.Form["txtSearch"].ToString());
    int maxrow = Convert.ToInt32(Request.Form["txtmaxrow"].ToString());
    string priceType = !string.IsNullOrWhiteSpace(price.priceType) && price.priceType.ToUpper().Equals("STA") ? "N" : "Y";

    // Get the price information
    var operationResult = priceBal.SearchPriceEnquiry(0, item, price.price_scheme, priceType, maxrow);            
    var priceEnquiryDomList = (List<PriceEnquiryDom>)operationResult[0].Result;

    // Check if we have something
    if (priceEnquiryDomList != null && priceEnquiryDomList.Count > 0)
    {
        // Parse the model.
        priceEnquiryModel = helper.ConvertDomToModel(priceEnquiryDomList[0]);
        // Prepare the list.
        priceEnquiryModel.PriceEnquiryModelList = new List<PriceEnquiryModel>();
        foreach (var priceEnquiryDom in priceEnquiryDomList)
        {
            var priceEnquiryModelListItem = helper.ConvertDomToModel(priceEnquiryDom);
            priceEnquiryModel.PriceEnquiryModelList.Add(priceEnquiryModelListItem);
        }
        Session["mainModel"] = priceEnquiryModel;
    }

    // Prepare product drop down list items if searched by product desc
    if (TempData.Count > 0 && TempData["Products"] != null)
    {
        var products = TempData["Products"] as List<ProductSearchByDescModel>;
        ViewBag.Products = products;
    }
    return View("Index", priceEnquiryModel);
}

This is the model on the View (while debugging) -

enter image description here

This is how I am rendering the model on the view -

enter image description here

This is the page after running -

enter image description here

Does anyone has any idea what is going on ? I have done the same thing on multiple pages and all run as expected.

Thanks in Advance. Rohit

ekad
  • 14,436
  • 26
  • 44
  • 46
Rohit
  • 1,520
  • 2
  • 17
  • 36
  • Would you show us action? Maybe some problem happens when you pass model action to view. – Mahedi Sabuj Jun 16 '15 at 21:24
  • Is there any JavaScript messing with those values? – beautifulcoder Jun 16 '15 at 21:24
  • @beautifulcoder, no there is not such js.. – Rohit Jun 16 '15 at 21:26
  • @MahediSabuj - On the view at time of rendering it has the values in model.. its just not showing on the page. See the second image, it does has values. – Rohit Jun 16 '15 at 21:27
  • 1
    You need to show your controller methods. Does you GET method have a parameter `PriceEnquiryModel` in the signature? Do a test by adding `
    @Model.description
    ` in the view to check its correct - if so, its a `ModelState` issue.
    –  Jun 16 '15 at 22:34
  • @StephenMuecke, `
    @Model.description
    ` this is working.. how to solve this model state issue ?
    – Rohit Jun 16 '15 at 22:41
  • @StephenMuecke, `
    @Model.description
    ` this is working.. how to solve this model state issue ?
    – Rohit Jun 16 '15 at 22:41
  • Like I said, your need to show your controller code –  Jun 16 '15 at 22:43
  • Updated the question. – Rohit Jun 16 '15 at 22:48

1 Answers1

2

The issue is that your method has parameter PriceEnquiryModel price but then you return a new instance of PriceEnquiryModel (named priceEnquiryModel). The process of model binding includes binding your model and adding its values to ModelState (along with any validation errors).

When you return the view, the html helper methods use the values from ModelState (not the models values) so attempting to change the values (which I assume is what priceEnquiryModel = helper.ConvertDomToModel(priceEnquiryDomList[0]); is doing) is ignored by the helpers.

For an explanation of why this is the default behavior, refer the second part of this answer

One option to call ModelState.Clear() before setting new values for the properties of PriceEnquiryModel

Community
  • 1
  • 1