2014-01-27 54 views
-1

我只是想在查看模塊的模型(即CSHTML)檢查數據和我的艱難是這樣MVC:從模型的數據傳遞到CSHTML的ASP.NET MVC

@model CCG.Models.RatingConverter 
     <table> 
      <tbody> 
       <tr> 
        @if (Model.ToString()!="A") 
        { 
        <td class="row" ><%- Rating %></td> 
        } 
       </tr> 
      </tbody> 
     </table>  

我得到Null Reference Exception錯誤..所以,請任何一個知道..

+0

由語句「@if(Model.ToString()! =「A」)「你的意思是你從控制器得到一個字符串? –

+0

雅Mahesh ..我讀取字符串值從...你 –

+0

你可以發佈你的控制器和你的RatingConverter類嗎?謝謝 – binard

回答

1

你需要從你的控制器通過一個視圖模型到您的視圖。

例如是這樣的:

var ratingConverter = new CCG.Models.RatingConverter(); 
//instanciate with data 

Return View("MyView", ratingConverter); 
4

首先,從控制器通過您的視圖模型這樣

public ActionResult ActionName() 
     { 
      //your code 
      return View(listautomation);    
     } 

然後將它綁定在你的視圖部分這樣

@model ViewModel.ListAutomation 

獲取視圖中的價值如下

<input type="text" id="id" value="@Model.ListAutomation " readonly="True"/> 
1

如果你正在從控制器的字符串,然後

寫控制器的方法類似

public ViewResult MyMethod(){ 
ViewBag.MyString="A"; 
    return View(); 
    } 

鑑於

<table> 
      <tbody> 
       <tr> 
        @if (ViewBag.MyString.ToString()!="A") 
        { 
        <td class="row" ><%- Rating %></td> 
        } 
       </tr> 
      </tbody> 
     </table> 
相關問題