简体   繁体   English

为什么我不能将此对象从视图传递到控制器方法?

[英]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()方法,并且该方法有两个参数:

  1. vulnId = Model.Id that is a long vulnId = Model.Id这是一个

  2. 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.

相关问题 如何将方法返回的结果从控制器传递到视图 - How can I pass the results returned by a method from controller to the view 我无法从视图中调用控制器中的action方法 - I can't to call action method in the controller from view 如何使用POST将复杂对象从视图传递到控制器-ASP.NET MVC - How can i pass complex object from view to controller with POST - ASP.NET MVC 如何将对象从我的视图传递到 ASP.NET MVC 中的控制器? - How can I pass an object from my view to my controller in ASP.NET MVC? 如何在 MVC5 中提交时将 object 从视图传递到不同的 controller? - How can I pass an object from a view to a different controller on submit in MVC5? 为什么我不能从通用对象运行以下方法? - Why can't I run the following method from a generic object? 将自定义视图对象从控制器传递到视图 - Pass Custom View Object from Controller to View 为什么我不能通过 List<Customer> 作为接受 List 的方法的参数<object> ? - Why can't I pass List<Customer> as a parameter to a method that accepts List<object>? 我可以在没有Form的情况下将模型从视图传递到控制器吗? - Can i pass a Model from view to controller without Form? 如何将数组从视图传递到控制器? - How can I pass an array from view to controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM