[英]Why I can't pass this object from a view to a controller method?
In a view I have something like this: 在视图中,我有这样的事情:
<ul data-role="listview" data-inset="true" data-theme="b" data-split-icon="delete">
@foreach (DataModel.Vulnerability.Fix item in Model.VulnerabilityFixes)
{
<li><a href="@Url.Action("Details", "Product", new { Title = item.Title })">
<h2>@item.Title</h2>
<table style="width: 100%">
<tr>
<th>Id</th>
<th>FixName</th>
<th>Vendor</th>
<th>Version</th>
</tr>
<tr>
<td>@MyHelper.decodeNull(item.Id)</td>
<td>@MyHelper.decodeNull(item.FixName)</td>
<td>@MyHelper.decodeNull(item.Vendor)</td>
<td>@MyHelper.decodeNull(item.Version)</td>
</tr>
</table>
</a>
<a href="@Url.Action("DeleteFix", "Editing", new { vulnId = Model.Id, fix = item })">Delete</a>
</li>
}
</ul>
As you can see it show a table and this table have a delete button that clicked call a DeleteFix() method on an EditingController class and this method take 2 paramethers that are: 如您所见,它显示一个表,并且该表具有一个删除按钮,单击该按钮会在EditingController类上调用调用DeleteFix()方法,并且该方法有两个参数:
vulnId = Model.Id that is a long vulnId = Model.Id这是一个长
the current item that is a DataModel.Vulnerability.Fix object. 当前项是DataModel.Vulnerability.Fix对象。
So this is the code of my DeleteFix() definied into my EditingController class: 因此,这是我的EditingController类中定义的DeleteFix()的代码:
public ActionResult DeleteFix(long vulnId = 0, DataModel.Vulnerability.Fix currentFix)
{
DataModel.Vulnerability.Fix model = new DataModel.Vulnerability.Fix();
manager.openConnection();
try
{
model = currentFix;
}
finally
{
manager.closeConnection();
}
return View(model);
}
The problem is that give me the following error on the signature of this method refered to the second input parameter (the DataModel.Vulnerability.Fix object). 问题是,在引用第二个输入参数( DataModel.Vulnerability.Fix对象)的此方法的签名上,出现以下错误。 It say to me that:
它对我说:
Error 14 Optional parameters must appear after all required parameters C:\\Develop\\EarlyWarning\\public\\Implementazione\\Ver2\\WebPortal\\WebPortal\\Controllers\\EditingController.cs 27 94 WebPortal
错误14可选参数必须出现在所有必需参数之后C:\\ Develop \\ EarlyWarning \\ public \\ Implementazione \\ Ver2 \\ WebPortal \\ WebPortal \\ Controllers \\ EditingController.cs 27 94 WebPortal
Why? 为什么? What am I missing?
我想念什么? What can I do to pass the previous DataModel.Vulnerability.Fix item object from my view to my DeleteFix() controller method?
如何将先前的DataModel.Vulnerability.Fix项对象从我的视图传递到DeleteFix()控制器方法?
Check your function signature: 检查您的功能签名:
public ActionResult DeleteFix(long vulnId = 0, DataModel.Vulnerability.Fix currentFix)
Swap the input parameters: 交换输入参数:
public ActionResult DeleteFix(DataModel.Vulnerability.Fix currentFix, long vulnId = 0)
The error is now gone. 错误现在消失了。 The reason is that all parameters that do not get a default value (
currentFix
) must appear before all parameters that do get a default value ( long vulnId = 0
) 原因是所有未获得默认值 (
currentFix
)的参数必须出现在所有获得默认值 ( long vulnId = 0
)的参数之前
update 更新
@Neel's answer is also correct, but not related to the error you mentioned. @Neel的答案也是正确的,但与您提到的错误无关。
It's not the cause of the error, but it's also something that needs to be fixed. 这不是错误的原因,但它也需要修复。 Assuming this isn't just a typo in the SO question :)
假设这不仅仅是SO问题中的错别字:)
make changes in you actionlink as below and put currentFix
instead of fix
and change the sequence as below :- 如下所示在您的actionlink中进行更改,并放置
currentFix
而不是fix
并按如下所示更改顺序:
(As flatter suggested ) (奉承建议)
public ActionResult DeleteFix(DataModel.Vulnerability.Fix currentFix, long vulnId = 0)
and in view 并鉴于
<a href="@Url.Action("DeleteFix", "Editing", new { currentFix = item, vulnId = Model.Id, })">Delete</a>
In C# you cannot declare an optional parameter BEFORE a non optional one. 在C#中,您不能在非可选参数之前声明可选参数。 Change your controller action to be:
将您的控制器操作更改为:
public ActionResult DeleteFix(DataModel.Vulnerability.Fix currentFix, long vulnId = 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.