2

So I got this webproject i'm working on, and in 4/5 views Im using

 @Html.ActionLink("Back to List", "Index", new { id=Model.ClientID})

All of the views return me to my list exept my "Create New View" This will also be the only view not to load, giving me the exeption of "System.NullReferenceException"

I'm confused on why this is the only view that won't let me pass a clientID to it ( since i need not only it but also the CountyID to create a new County, And more so telling me that it is null.

If i remove the line of code above my code runs fine ( exept adding my 2 ID fields into the create view obviously ) wich made me think it may be my controller.

Here is my action from givin controller for create

    // GET: /County/Create
    public ActionResult Create()
    {
        return View();
    }

for comparison here is an edit action in the same controller

    public ActionResult Edit(int id = 0)
    {
        dbCounty countys = db.Countys.Find(id);
        if (countys == null)
        {
            return HttpNotFound();
        }
        return View(countys);
    }

I've also tryed adding this code to the create new actionlink wich is when i get this error

HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily         unavailable.
Most likely causes:

The directory or file specified does not exist on the Web server.
The URL contains a typographical error.
A custom filter or module, such as URLScan, restricts access to the file.

What am i doing wrong here ....

My project is build on a hierachy model, One Client Many Countys ( if you need more code let me know )

thanks in advance.

Tip:

there must be a reason ( my assumption ) why when removing this line of code it works ( so it must be this line of code ?? ) - must be a different way of passing it ( clientID does have a value of 1 )

 @Html.ActionLink("Back to List", "Index", new { id=Model.ClientID})

Edit Index Controller needed :

    public ActionResult Index([Bind(Prefix="id")] int CID=0)
    {
        var clnt = db.Clients.Find(CID);

        if (clnt != null)
        { 
            return View(clnt);
        }

        return HttpNotFound();

     }

EDIT: new create action from Countys Controller

    public ActionResult Create(int id=0)
    {
        dbCounty countys = db.Countys.Find(id);
        if (countys == null)
        {
            return HttpNotFound();
        }
        return View(countys);
    }

i have also tryed running

    public ActionResult Create(int id=0)
      {
        dbClient Client = db.Clients.Find(id);
        if (Client == null)
        {
            return HttpNotFound();
        }
        return View(Client);
    }

( since i am passing a clientID - the way the models are built it should add a county id while creating this new row in the database with the clientID(passedVariable)

4

3 回答 3

2

You don't seem to be passing a model to the Create view. In your Create controller, you need something like the following, where Client is an object that has the property ClientID.

public ActionResult Create()
{
    ...
    return View(Client);
}

EDIT:

To clarify, in your Create View, the Model is null, because you didn't pass it one in the controller. Take a look at the controllers that work, and see what it's passing to the View. You'll need to do something similar for Create.

于 2013-07-02T13:41:21.950 回答
1

I will try to sum up your problem. You said that if you remove

@Html.ActionLink("Back to List", "Index", new { id=Model.ClientID})

There is no errror, and the error happens when your view is generating. This two facts means that in 99% your Model is null

于 2013-07-02T13:41:59.703 回答
0

Check that in

@Html.ActionLink("Back to List", "Index", new { id=Model.ClientID})

Model is not null, otherwise you will get null reference exception

于 2013-07-02T13:44:54.523 回答