mvc:将模型中的数据传递给cshtml Asp .net MVC

时间:2014-01-27 09:21:55

标签: c# asp.net-mvc viewmodel models

我只是想检查来自模型视图模块中的数据(即CSHTML),我的尝试就像这样

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

我收到Null Reference Exception错误..所以请任何人知道..

4 个答案:

答案 0 :(得分:4)

首先从控制器传递viewmodel,如此

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

然后将其绑定在您的视图部分中

@model ViewModel.ListAutomation

像这样获取值

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

答案 1 :(得分:1)

您需要将控制器中的视图模型传递给视图。

例如:

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

Return View("MyView", ratingConverter);

答案 2 :(得分: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> 

答案 3 :(得分:0)

尝试此解决方案-

delete from mytable t
using (
    select snapshot_date, device_id, min(snapshot_id) snapshot_id
    from mytable 
    group by snapshot_date, device_id
) t1
where 
    t.snapshot_date = t1.snapshot_date
    and t.device_id = t1.device_id
    and t.snapshot_id < t1.id