简体   繁体   中英

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:

  1. vulnId = Model.Id that is a long

  2. the current item that is a DataModel.Vulnerability.Fix object.

So this is the code of my DeleteFix() definied into my EditingController class:

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

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?

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 )

update
@Neel's answer is also correct, but not related to the error you mentioned.
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 :)

make changes in you actionlink as below and put currentFix instead of fix and change the sequence as below :-

(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. Change your controller action to be:

public ActionResult DeleteFix(DataModel.Vulnerability.Fix currentFix, long vulnId = 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM