2014-11-15 56 views
2

我試圖格式化價格標籤,所以價格總是在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> 
} 
+0

請問你的模型有一個'Price'財產?並且是'View'中設置的模型。 – christiandev

+0

@christiandev首先讓我首先說這是我第一個MVC項目。 public decimal Price {get;組; }就是我在模型中設置的,所以我相信這意味着它正確地設置爲價格屬性。 – user3610374

+0

向我們展示'Controller'和'View' – christiandev

回答

1

我不知道什麼控制器類似,但採取這種測試版本。 ..

控制器

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

我創建了一個產品類爲例..

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) 
+0

注意,這是創建2個元素具有相同的id(無效html)和'Value = String.Format(「{0:C}」,Model.Price)''是沒有必要的(它可以只是'@Html.TextBoxFor (m => m.Price,「{0:C}」)' –

+0

我可以在索引方法中使用代碼,因爲這看起來我需要在我的代碼中,但是我的代碼根據用戶選擇的選項創建價格價格會留在內存中嗎? – user3610374

+0

我不明白爲什麼我需要使用文本框和標籤,如果我所做的只是顯示價格。我不應該只需要一個標籤和格式? – user3610374