试图更改控制器内的模型数据

时间:2014-11-24 10:52:46

标签: c# asp.net-mvc

我的模型中有一个价格属性,如果客户选择不同的选项并且我在下面包含了我的代码,我想要更改。我想永久地更改模型中的数据,我尝试了几种不同的方法,但我似乎没有得到任何结果。当我在页面发布时尝试访问模型数据时,price属性保持原始值。任何人都可以帮助我理解为什么我的代码不起作用吗?

控制器:

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

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

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

        // do something with value and return a decimal
        return Json(new { currentPrice = currentPrice.ToString("C"), unformattedCurrentPrice = currentPrice.ToString() }, JsonRequestBehavior.AllowGet);
    }

[HttpPost]
    public ActionResult Contact(HomeworkHelpers.Models.Order c)
    {

        if (ModelState.IsValid)
        {
            try
            {
            }
            catch (Exception)
            {
                return View("Error");
            }
        }
        return View("Index", c);
    }

查看:

@model HomeworkHelpers.Models.Order

$('#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) {
                    $('#priceLabel').html(data.currentPrice);
                    $('#Price').val(data.unformattedCurrectPrice);
                }
            });
        });

@using (Html.BeginForm("Contact", "Home", FormMethod.Post))
{
@Html.ValidationSummary(true)
<div class="row">
    <h2 id="priceLabel">
        @Html.DisplayFor(model => model.Price)
    </h2>
    @Html.HiddenFor(model => model.UnFormattedPrice)
    @Html.HiddenFor(model => model.Price)
    <input type="submit" value="Submit" />
    <input type="reset" value="Reset" />
</div>
}

型号:

 namespace HomeworkHelpers.Models
{
public class Order
{
    [DataType(DataType.Currency)]
    [DisplayFormat(DataFormatString = "{0:C}")]
    public decimal Price { get; set; }

    public string UnFormattedPrice { get; set; }
}
}

0 个答案:

没有答案