如何在MVC .Net的视图文件夹中解决此ActionLink错误 [英] How do I solve this ActionLink error in the view folder in MVC .Net

查看:78
本文介绍了如何在MVC .Net的视图文件夹中解决此ActionLink错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI全部,



在我的控制器文件夹中,我有一个动作列表方法,它返回一个这样的书籍对象:



HI All,

In my controllers folder I got an action list method that returns a book object like this:

public ActionResult Test()
       {
           Book book = new Book()
           {

               ID = 1,
               BookName = "Abraham Life",
               AuthorName = "King",
               ISBN = "NA"
           };
           return View();

       }





当我运行项目时,我一直收到空的异常错误此操作的测试视图文件测试方法。



更具体地说,错误来自actionlink,如:



when I run the project i keep getting a null exception error in the Test view file for this action Test method.

To be more specific the error comes from the actionlink like:

<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>





当我将鼠标悬停时指针在第二个编辑它说不能解决行动编辑。

有什么我做错了吗?一些请帮忙。







和我的模型文件夹我已经定义了这样的属性:





and when I hover my mouse pointer on the second edit it says cannot resolve action Edit.
is there anything Im doing wrong?? some please help.



and my model folder I have defined the properties like so:

public class Book
   {
       public int ID { get; set; }
       public string BookName { get; set; }
       public string AuthorName { get; set; }
       public string ISBN { get; set; }
   }

推荐答案

您没有将对象传递给视图。



在您的Index操作中,您可以通过将要传递的对象传递给视图来执行此操作:

You're not passing an object to the View.

In your Index action, you do this by passing the object you want to pass to the view like this:
return View(Book);





但是!通常,您不会将实体对象传递给视图。你传递了一个视图模型类实例。



为什么?因为传递视图模型,您还可以传递视图可能需要的其他信息,例如下拉列表或其他UI或状态信息的值,实体对象不能包含这些信息。



BUT! Normally you don't pass entity objects to the view. You pass a view model class instance.

Why? Because passing a view model you can also pass in other information that the view may need, like values for dropdowns or other UI or state information, that the entity object cannot contain.


这篇关于如何在MVC .Net的视图文件夹中解决此ActionLink错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆