Being new to ASP MVC, I met the following problem.
作为ASP MVC的新手,我遇到了以下问题。
I have a list of "repeating" controls on my page, which are presented by the following Razor code:
我的页面上有一个“重复”控件列表,由以下Razor代码显示:
@model BankBLL.Interfaces.ISecureFolder
...(some irrelevant code here)
<header><h3 >Commitee list</h3></header>
@foreach (var commitee in Model.Commitees)
{
<a href="@Url.Action("CommiteePage", "SecureFolder", commitee)">
<div class="commiteeButtonImageContainer">@commitee.Name</div>
<img src="~/Images/CommiteeButtonImage.png"/>
</a>
}
Model.Commitees here is a List of ICommitee objects, that means that I am trying to "bind" each Url.Action to a corresponding ICommitee commitee object. However, when it comes to my controller action:
这里的Model.Commite是一个ICommitee对象列表,这意味着我试图将每个Url.Action“绑定”到相应的ICommitee委托对象。但是,当涉及到我的控制器操作时:
public ActionResult CommiteePage(ICommitee commitee)
{
return View("CommiteePage", commitee);
}
looks like I am making it a wrong way, because application returns "Cannot create an instance of an interface." error, that means that application is unable to retreive required commitee object when the action link is clicked.
看起来我正在以错误的方式,因为应用程序返回“无法创建接口的实例”。错误,这意味着单击操作链接时应用程序无法检索所需的commitee对象。
Is there a way to bind each row "item datacontext" (ICommitee object in this case) to correspoding Url.Action?
有没有办法将每一行“item datacontext”(在本例中为ICommitee对象)绑定到对应的Url.Action?
2 个解决方案
#1
0
The main problem is that the default model binder cannot create an instance of an interface. Try to be more specific, i.e. public ActionResult CommiteePage(ImplementedCommiteeType commitee)
. You can also create a CommiteeViewModel: ICommitee
class in which you can transport your structures (in Controllers and Views only).
主要问题是默认模型绑定器无法创建接口的实例。尝试更具体,即公共ActionResult CommiteePage(ImplementedCommiteeType commitee)。您还可以创建一个CommiteeViewModel:ICommitee类,您可以在其中传输结构(仅限控制器和视图)。
Or you can create your own model binder which knows what to implement. This is slightly more complicated.
或者您可以创建自己的模型绑定器,它知道要实现什么。这稍微复杂一些。
#2
1
Unfortunately could not post it earlier due to reputation regulations.
遗憾的是,由于声誉规定,无法提前发布。
Finally resolved this issue due to good explanation at: HTML.ActionLink method
最后通过以下方式解决了这个问题:HTML.ActionLink方法
When you try to pass an argument from Url.Action or Html.ActionLink - you have to specify explicitly the final "null" argument responsible for html arguments.
当您尝试从Url.Action或Html.ActionLink传递参数时 - 您必须明确指定负责html参数的最终“null”参数。
In my case the following code works correctly:
在我的情况下,以下代码正常工作:
slightly changed controller action (now receives just name instead of commitee object itself)
稍微改变了控制器动作(现在只接收名称而不是commitee对象本身)
public ActionResult CommiteePage(string commiteeName)
{
return View("CommiteePage", SecureFolder.Commitees.First(o=>o.Name == commiteeName));
}
and changed syntax for html calling this action:
并更改了html调用此操作的语法:
@foreach (var commitee in Model.Commitees)
{
<a href="@Url.Action("CommiteePage", "SecureFolder", new { commiteeName=commitee.Name }, null)">
<div class="commiteeButtonImageContainer">@commitee.Name</div>
<img src="~/Images/CommiteeButtonImage.png"/>
</a>
}
Now view correctly passes the name of selected commitee to controller so that I can redirect to corresponding commitee view.
现在查看正确地将所选委托者的名称传递给控制器,以便我可以重定向到相应的委托人视图。
Thank you all for helping to resolve this issue!
谢谢大家帮忙解决这个问题!
#1
0
The main problem is that the default model binder cannot create an instance of an interface. Try to be more specific, i.e. public ActionResult CommiteePage(ImplementedCommiteeType commitee)
. You can also create a CommiteeViewModel: ICommitee
class in which you can transport your structures (in Controllers and Views only).
主要问题是默认模型绑定器无法创建接口的实例。尝试更具体,即公共ActionResult CommiteePage(ImplementedCommiteeType commitee)。您还可以创建一个CommiteeViewModel:ICommitee类,您可以在其中传输结构(仅限控制器和视图)。
Or you can create your own model binder which knows what to implement. This is slightly more complicated.
或者您可以创建自己的模型绑定器,它知道要实现什么。这稍微复杂一些。
#2
1
Unfortunately could not post it earlier due to reputation regulations.
遗憾的是,由于声誉规定,无法提前发布。
Finally resolved this issue due to good explanation at: HTML.ActionLink method
最后通过以下方式解决了这个问题:HTML.ActionLink方法
When you try to pass an argument from Url.Action or Html.ActionLink - you have to specify explicitly the final "null" argument responsible for html arguments.
当您尝试从Url.Action或Html.ActionLink传递参数时 - 您必须明确指定负责html参数的最终“null”参数。
In my case the following code works correctly:
在我的情况下,以下代码正常工作:
slightly changed controller action (now receives just name instead of commitee object itself)
稍微改变了控制器动作(现在只接收名称而不是commitee对象本身)
public ActionResult CommiteePage(string commiteeName)
{
return View("CommiteePage", SecureFolder.Commitees.First(o=>o.Name == commiteeName));
}
and changed syntax for html calling this action:
并更改了html调用此操作的语法:
@foreach (var commitee in Model.Commitees)
{
<a href="@Url.Action("CommiteePage", "SecureFolder", new { commiteeName=commitee.Name }, null)">
<div class="commiteeButtonImageContainer">@commitee.Name</div>
<img src="~/Images/CommiteeButtonImage.png"/>
</a>
}
Now view correctly passes the name of selected commitee to controller so that I can redirect to corresponding commitee view.
现在查看正确地将所选委托者的名称传递给控制器,以便我可以重定向到相应的委托人视图。
Thank you all for helping to resolve this issue!
谢谢大家帮忙解决这个问题!