我試圖格式化價格標籤,所以價格總是在0.00格式,我看到一個類似的問題,使用類似的代碼,如下所示。我得到一個object reference not set to an instance of an object
錯誤,我知道這是因爲我打電話Model.Price
,但我很新的MVC,我不明白我如何設置頁面加載前Model.Price
。在LabelFor()控件中格式化文本
@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);
}
這裏是查看代碼:
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="row">
@Html.LabelFor(model => model.Name, "Name:")
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="row">
@Html.LabelFor(model => model.Email, "Email:")
@Html.TextAreaFor(model => model.Email, new { id = "email" })
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="row">
@Html.LabelFor(model => model.Topic, "Topic:")
@Html.EditorFor(model => model.Topic)
@Html.ValidationMessageFor(model => model.Topic)
</div>
<div class="row">
@Html.LabelFor(model => model.Subject, "Subject:")
@Html.DropDownListFor(model => model.Subject, (SelectList)ViewBag.subject, "--Select--", new { id = "subjectList" })
</div>
<div class="row">
@Html.LabelFor(model => model.Style, "Style:")
@Html.DropDownListFor(model => model.Style, (SelectList)ViewBag.paperStyle, "--Select--", new { id = "paperStyleList" })
</div>
<div class="row">
@Html.LabelFor(model => model.DocumentType, "Document Type:")
@Html.DropDownListFor(model => model.DocumentType, (SelectList)ViewBag.documentType, "--Select--", new { id = "documentTypeList" })
</div>
<div class="row">
@Html.LabelFor(model => model.AcademicLevel, "Academic Level:")
@Html.DropDownListFor(model => model.AcademicLevel, (SelectList)ViewBag.academicLevel, "--Select--", new { id = "academicLevelList" })
</div>
<div class="row">
@Html.LabelFor(model => model.NumberOfPages, "Number of Pages/Words:")
@Html.DropDownListFor(model => model.NumberOfPages, (SelectList)ViewBag.numberOfPages, "--Select--", new { id = "numberOfPagesList" })
</div>
<div class="row">
@Html.LabelFor(model => model.NumberOfSources, "Number of Sources:")
@Html.DropDownListFor(model => model.NumberOfSources, (SelectList)ViewBag.numberOfSources, "--Select--", new { id = "numberOfSourcesList" })
</div>
<div class="row">
@Html.LabelFor(model => model.Urgency, "Urgency:")
@Html.DropDownListFor(model => model.Urgency, (SelectList)ViewBag.urgency, "--Select--", new { id = "urgencyList" })
</div>
<div class="row">
@Html.LabelFor(model => model.Spacing, "Spacing:")
@Html.RadioButtonFor(model => model.Spacing, "Single") Single
@Html.RadioButtonFor(model => model.Spacing, "Double") Double
</div>
<div class="row">
@Html.LabelFor(model => model.Requirements, "Requirements:")
@Html.TextAreaFor(model => model.Requirements)
</div>
<div class="row">
@Html.DropDownListFor(model => model.Currency, (SelectList)ViewBag.currency, null, new { id = "currencyList" })
<h2>
@Html.LabelFor(model => model.Price, "{0:C}", new { id = "priceLabel" })
</h2>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</div>
}
請問你的模型有一個'Price'財產?並且是'View'中設置的模型。 – christiandev
@christiandev首先讓我首先說這是我第一個MVC項目。 public decimal Price {get;組; }就是我在模型中設置的,所以我相信這意味着它正確地設置爲價格屬性。 – user3610374
向我們展示'Controller'和'View' – christiandev