Suriya Prakash.T
Suriya Prakash.T

Reputation: 11

mvc : Pass data from Models to cshtml Asp .net MVC

I just want to check the data from Models in View module (I.e.CSHTML) and my trying is like this

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

I get Null Reference Exceptionerror.. So please any one know..

Upvotes: 0

Views: 36416

Answers (4)

nkyek
nkyek

Reputation: 1

Try this solution -

@model VievModel.MyView

Return View("MyView", ratingConverter);

Upvotes: 0

Mahesh Reddy
Mahesh Reddy

Reputation: 356

If you are getting a string from controller then

write controller method like

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

in view

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

Upvotes: 0

Sajeev
Sajeev

Reputation: 197

First you pass your viewmodel from controller like this

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

then bind it in your view part like this

@model ViewModel.ListAutomation

Get the value in view like this

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

Upvotes: 5

Mark van Straten
Mark van Straten

Reputation: 9425

You need to pass a viewmodel from your controller to your view.

For instance something like this:

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

Return View("MyView", ratingConverter);

Upvotes: 1

Related Questions