MVC - ActionLink - Словарь параметров содержит пустую запись для параметра 'id' ненулевого типа - PullRequest
0 голосов
/ 20 марта 2020

У меня возникла проблема с сопоставлением идентификатора из моего представления моему контроллеру.

Когда пользователь выбирает редактирование, он будет направлен на страницу редактирования с пропущенным идентификатором выбранной строки. через контроллер, однако он выдает сообщение об ошибке «Словарь параметров содержит пустую запись для параметра« id »ненулевого типа». Даже когда я пытаюсь жестко закодировать «id = #», он все равно считается нулевым.

Мое представление:

@model Mvc4_Development_Test.Models.Customer.CustomerDTO
@using Mvc4_Development_Test.Models.Customer

@{
    ViewBag.Title = "Customers Index";
    Mvc4_Development_Test.Models.Customer.CustomerListBO tblHeader = Model.CustomerList.FirstOrDefault( );
}

<h2 style="text-align:center">Customers Index</h2>

<br />

@using ( Html.BeginForm( "ViewCustomer" , "Customer" , FormMethod.Get , new { id = "fm_SearchFilter" } ) )

{
    <fieldset style="margin-left: 110px;">
        <div class="editor-label">
            @Html.LabelFor( model => model.searchFunction.Customer_Name )
        </div>
        <div class="editor-field">
            @Html.EditorFor( model => model.searchFunction.Customer_Name )


            @Html.DropDownListFor( model => model.searchFunction.Stat_ID , new SelectList( Model.StatList , "Stat_ID" , "Stat_Desc" , Model.searchFunction.Stat_ID ) )

            <input type="submit" value="Search" style="margin-left: 10px;" />
            <a href="./CreateCustomer"><input type="button" value="Create Customer" /></a>
        </div>
    </fieldset>
}
@*Display Table Data*@
<table style="margin-left: auto; margin-right: auto; background-color: white; font-weight: 500;">
    <tr>
        @*Display Table Header Names*@
        <th>@Html.DisplayNameFor( m => tblHeader.Customer_Name )</th>
        <th>@Html.DisplayNameFor( m => tblHeader.Customer_School )</th>
        <th>@Html.DisplayNameFor( m => tblHeader.Customer_Email )</th>
        <th>@Html.DisplayNameFor( m => tblHeader.Service_Name )</th>
        <th>@Html.DisplayNameFor( m => tblHeader.Customer_RegDate )</th>
        <th>@Html.DisplayNameFor( m => tblHeader.Staff_Name )</th>
        <th>@Html.DisplayNameFor( m => tblHeader.Stat_ID )</th>
        <th>Options</th>
        <th>@Html.DisplayNameFor( m => tblHeader.Date_Edited )</th>
    </tr>

    @* Display a table of Customers *@
    @foreach ( Mvc4_Development_Test.Models.Customer.CustomerListBO member in Model.CustomerList )
    {
        <tr>
            @*Customers Title and Name*@
            <td>
                @Html.DisplayFor( modelItem => member.Title_Desc )
                @Html.DisplayFor( modelItem => member.Customer_Name )
            </td>
            @*Customers School Choice*@
            <td>
                @Html.DisplayFor( modelItem => member.Customer_School )
            </td>
            @*Customers Email Address*@
            <td>
                @Html.DisplayFor( modelItem => member.Customer_Email )
            </td>
            @*Display List of Customer Services*@
            <td>
                <ul style="list-style: none;">
                    @for ( var x = 0; x < member.GetCustomerServiceList.Count; x++ )
                    {
                        <li>@Html.DisplayFor( modelItem => member.GetCustomerServiceList [ x ].Service_Name )</li>
                    }
                </ul>
            </td>
            @*Customer Registration Dates*@
            <td>
                @Html.DisplayFor( modelItem => member.Customer_RegDate )
            </td>
            @*Contacting Staff Member*@
            <td>
                @Html.DisplayFor( modelItem => member.Staff_Name )
                <br />
                @Html.DisplayFor( modelItem => member.Department_Name )
            </td>
            @*Customers Status Description*@
            <td>
                @Html.DisplayFor( modelItem => member.Stat_Desc )
            </td>
            @*Display Button Options*@
            <td>
                @*Customer Block/Unblock Button*@
                <button>
                    @Html.ActionLink( ( member.Stat_ID == ( int )Status.Active ) ? "Block" : "Unblock" , "CustomerStatus" , "Customer" , new { id = member.Customer_ID , status = @member.Stat_ID } , null )
                </button>
                @*Customer Edit Button*@
                <button>
                    @Html.ActionLink( "Edit" , "EditCustomer" , "Customer" , new { id = member.Customer_ID } )
                </button>
                @*Customer Delete Button*@
                <button>
                    @Html.ActionLink( "Deleted" , "CustomerStatus" , "Customer" , new { id = @member.Customer_ID , status = ( int )Status.Deleted } , null )
                </button>
            </td>
            <td>
                @Html.DisplayFor( modelItem => member.Date_Edited )
            </td>
        </tr>
    }
</table>

Мой контроллер клиента:

public ActionResult EditCustomer( int id )
        {

            CustomerDTO dto = new CustomerDTO( );

            var std = dto.CustomerList.Where( s => s.Customer_ID == id ).FirstOrDefault( );

            if ( id > 0 ) { 

                dto.Customer = cDAL.GetCustomerByID(id );

                return View( nameof( EditCustomer ) , dto.Customer );
            }
            else
            {
                return View( nameof( ViewCustomer ));
            }
        }

Моя модель

public int Customer_ID { get; set; }

public string Customer_Name { get; set; }

public string Customer_School { get; set; }

Я не могу иметь это поле как обнуляемое как это идентификатор в базе данных.

Любая помощь приветствуется.

1 Ответ

0 голосов
/ 23 марта 2020

Я нашел ответ, добавив ноль, где рядом со значением IE объекта:

<button>
@Html.ActionLink( "Edit" , "EditCustomer" , "Customer" , new { id = member.Customer_ID }, null )
</button>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...