在LabelFor()控件中格式化文本

时间:2014-11-15 09:42:59

标签: c# asp.net-mvc-3

我试图格式化价格标签,因此价格始终为 0.00格式,我看到了一个类似的问题,使用了与我的相似的代码。我收到object reference not set to an instance of an object错误,我知道这是因为我打电话给Model.Price,但我对MVC很新,我不明白我是如何设置{在页面加载之前{1}}。

Model.Price

这是控制器代码:

@Html.LabelFor(model => model.Price, "0.00", new { id = "priceLabel", Value = String.Format("{0:C}", Model.Price) })

以下是查看代码:

[HttpGet]
    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        // set the paper style list
        List<SelectListItem> styles = new List<SelectListItem>();
        ViewBag.paperStyle = new SelectList(styles, "Value", "Text");

        // set the subject list
        List<SelectListItem> subjects = new List<SelectListItem>();
        ViewBag.subject = new SelectList(subjects, "Value", "Text");

        // set the number of pages list
        List<SelectListItem> numberOfPages = new List<SelectListItem>();
        ViewBag.Urgency = new SelectList(urgency, "Value", "Text");

        // set the document type list
        List<SelectListItem> documentTypes = new List<SelectListItem>();
        ViewBag.documentType = new SelectList(documentTypes, "Value", "Text");

        // set the academic level list
        List<SelectListItem> academicLevel = new List<SelectListItem>();
        ViewBag.academicLevel = new SelectList(academicLevel, "Value", "Text");

        // set the number of sources list
        List<SelectListItem> numberOfSources = new List<SelectListItem>();
        ViewBag.numberOfSources = new SelectList(numberOfSources, "Value", "Text");

        // set the currency list
        List<SelectListItem> currencies = new List<SelectListItem>();
        currencies.Add(new SelectListItem() { Text = "$", Value = "USD", Selected = true });
        currencies.Add(new SelectListItem() { Text = "£", Value = "GBP", Selected = false });
        currencies.Add(new SelectListItem() { Text = "€", Value = "EUR", Selected = false });

        ViewBag.currency = new SelectList(currencies, "Value", "Text");

        return View();
    }

    [HttpPost]
    public ActionResult Contact(WritingAppModel c)
    {
        if (ModelState.IsValid)
        {
            // send the email and process the payment

            // if payment is ready then send email
            if (isPaymentReady())
            {
                // send email

            }
        }
        return View();
    }

public class modelData
    {
        public string documentType { get; set; }
        public string numberOfPages { get; set; }
        public string urgency { get; set; }
    }

    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);
                }
            }
        }

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

1 个答案:

答案 0 :(得分:1)

我不确定控制器是什么样的,但是拿这个测试版......

<强>控制器

 public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new Product {Price = 9.99m};
            return View(model);
        }
    }

我已经创建了一个Product类作为示例..

 public class Product
    {
        public decimal Price { get; set; }
    }

您的view将需要引用model

@model WebApplication2.Controllers.Product

@Html.LabelFor(model => model.Price, new { id = "priceLabel" })
@Html.TextBoxFor(model => model.Price, new { id = "priceLabel", 
                              Value = String.Format("{0:C}", Model.Price) })

您可以从控制器中看到构建模型的位置并将其传递给视图。

var model = new Product {Price = 9.99m};
return View(model);

LabelFor将为实际属性生成标签,而不是它的值。

编辑:根据评论。

如果您只想显示属性......

@String.Format("{0:C}", Model.Price)

或。正如史蒂夫所说......

<强>模型

[DisplayFormat(DataFormatString = "{0:C}")]
public decimal Price { get; set; }

查看

@Html.DisplayFor(model => model.Price)