你调用的对象是空的。 〜@ Html.ActionLink期间

时间:2021-03-21 16:55:03

I have a create action and a edit action in a Reviews Controller.

我在评论控制器中有一个创建操作和一个编辑操作。

My Create ActionLink is:

我的Create ActionLink是:

@Html.ActionLink("Create New", "Create", new { Id = Model.Id })

My create action is :

我的创建动作是:

    [HttpGet]
    public ActionResult Create(int Id)
    {
        return View();
    }

My Edit ActionLink is:

我的编辑ActionLink是:

  @Html.ActionLink("Edit", "Edit", new { id=item.Id }

my edit is:

我的编辑是:

    [HttpGet]
    public ActionResult Edit(int id)
    {
        var model = _db.Reviews.Find(id);
        return View(model);
    }

In my edit view, I have a Action Link called "Back to List" which is:

在我的编辑视图中,我有一个名为“返回列表”的动作链接,它是:

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

It works and takes me back to where I came from...

它起作用并带我回到我来自的地方......

In my create view, when I put the same thing, I get error message that is in the heading. that Id does not have a value or is null.. So Model.RestaurantId does not have a value..

在我的创建视图中,当我放置相同的东西时,我收到标题中的错误消息。 Id没有值或为null ..所以Model.RestaurantId没有值..

If I hard code a value ,it works such as:

如果我硬编码一个值,它的工作原理如下:

  @Html.ActionLink("Back to List", "Index", "Reviews", new { id = 1 }, null)

What could I be doing wrong...

我能做错什么......

I am essentially trying to follow Scott Allens MVC4 tutorial. I am unable to understand why this is happening. I have a reviews controller. Can some one give me suggestions?

我基本上试图遵循Scott Allens的MVC4教程。我无法理解为什么会这样。我有一个评论控制器。有人可以给我一些建议吗?

Thanks.

2 个解决方案

#1


0  

Have a viewmodel for your create view, with a property to store the source /parent id and use that as needed.

为您的创建视图设置一个viewmodel,其中包含一个存储源/父ID的属性,并根据需要使用它。

public classs CreateReviewVM
{
   public int SourceID { set;get;}
}

and in your GET action

并在你的GET行动中

[HttpGet]
public ActionResult Create(int Id)
{ 
   var vm=new CreateReviewVM { SourceID=id };
   return View(vm);
}

and your create view(create.cshtml) which is strongly typed to your CreateReviewVM

以及您的CreateReviewVM强类型的创建视图(create.cshtml)

@model CreateReviewVM
@Html.ActionLink("Back to List", "Index", "Reviews", 
                                              new { id = Model.SourceID }, null)

#2


0  

Turns out, I can use:

事实证明,我可以使用:

    @Html.ActionLink("Back to List", "Index", new { id=Request.Params["restaurantId"] }, null)

and it will populate the value.

它会填充价值。

#1


0  

Have a viewmodel for your create view, with a property to store the source /parent id and use that as needed.

为您的创建视图设置一个viewmodel,其中包含一个存储源/父ID的属性,并根据需要使用它。

public classs CreateReviewVM
{
   public int SourceID { set;get;}
}

and in your GET action

并在你的GET行动中

[HttpGet]
public ActionResult Create(int Id)
{ 
   var vm=new CreateReviewVM { SourceID=id };
   return View(vm);
}

and your create view(create.cshtml) which is strongly typed to your CreateReviewVM

以及您的CreateReviewVM强类型的创建视图(create.cshtml)

@model CreateReviewVM
@Html.ActionLink("Back to List", "Index", "Reviews", 
                                              new { id = Model.SourceID }, null)

#2


0  

Turns out, I can use:

事实证明,我可以使用:

    @Html.ActionLink("Back to List", "Index", new { id=Request.Params["restaurantId"] }, null)

and it will populate the value.

它会填充价值。