dimanche 16 novembre 2014

Change Model Data In a Controller Method


Vote count:

0




I'm trying to change data in my model based on choices that a user makes in the view. I have the ajax query below that grabs the drop down values selected and passes them to the controller. I'm trying to change the price value in the model and save it permanently so I can display the price to the user. Currently it calls the method correctly and I inserted a breakpoint and I can see that it has the current price in the value but when the method returns, it doesn't display anything at all. I'm new to MVC so I appreciate any help. Thanks!



$('#urgencyList').change(function () {
var modelData = {
documentType: $("#documentTypeList").val(),
urgency: $("#urgencyList").val(),
numberOfPages: $("#numberOfPagesList").val()
};
$.ajax({
type: "GET",
data: modelData,
url: "/Home/getNewPrice",
async: true,
success: function (data) {
document.getElementById('priceLabel').innerHTML = data.currentPrice;
}
});
});

@Html.DisplayFor(model => model.Price, new { id = "priceLabel" })


Here is my model:



[Required(ErrorMessage = "Price is Required.")]
[Range(0.01, 10000.00, ErrorMessage = "Your quote is not complete because you haven't completed all of the steps.")]
[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "{0:C}")]
public decimal Price { get; set; }


Here is my controller code:



public JsonResult getNewPrice(modelData dropdownValues)
{
// check for urgency first since that is the base price
if (dropdownValues.urgency != null)
{
currentPrice = Convert.ToDecimal(dropdownValues.urgency);

if (dropdownValues.documentType != null)
{
currentPrice = currentPrice + Convert.ToDecimal(dropdownValues.documentType);

if (dropdownValues.numberOfPages != null)
{
currentPrice = currentPrice * Convert.ToInt16(dropdownValues.numberOfPages);
}
}
}

currentPrice = Math.Round(currentPrice, 3, MidpointRounding.AwayFromZero);
model.Price = currentPrice;
// do something with value and return a decimal
return Json(new { currentPrice = model.Price }, JsonRequestBehavior.AllowGet);
}


asked 40 secs ago







Change Model Data In a Controller Method

Aucun commentaire:

Enregistrer un commentaire