使用Html.HiddenFor asp .net mvc获取每个元素的相同名称

时间:2016-03-19 11:24:03

标签: c# asp.net asp.net-mvc model-binding

我正在开发一个ASP .NET MVC项目,我正在使用Html.HiddenFor呈现项目列表,但问题是当所有呈现的项目时,我得到带有前缀的每个隐藏输入的名称 item. ,我想省略该项目。从每个隐藏的输入。我试过很多接近,但无法做到。

这是用于呈现项目列表的代码:

@foreach (var item in Model.Appointments)
{
    using (Html.BeginForm("GetAppointment", "Appointment", FormMethod.Post, htmlAttributes: new { @class = "form-horizontal", role = "form" }))
    {
        <tr>
            <td class="hidden">
                @Html.Hidden(item.DoctorId, "", htmlAttributes: new { @name = item.DoctorId })
                @Html.Hidden(item.DoctorName, "", htmlAttributes: new { @name = item.DoctorName })
                @Html.HiddenFor(modelItem => item.StartDate, htmlAttributes: new { @name = item.StartDate })
                @Html.HiddenFor(modelItem => item.EndDate, htmlAttributes: new { @name = item.EndDate })
                @Html.HiddenFor(modelItem => item.Day, htmlAttributes: new { @name = item.Day})
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DoctorName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.StartDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.EndDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Day)
            </td>
            <td>
                <input type="submit" value="Get Appointment" class="btn btn-success" />
            </td>
        </tr>
    }
}

以下是我每个项目的内容:

<td class="hidden">
    <input id="a05011d6-cdbd-46b2-9f72-ee582795e42a" name="a05011d6-cdbd-46b2-9f72-ee582795e42a" type="hidden" value="" />
    <input id="Uzair_Iqbal" name="Uzair Iqbal" type="hidden" value="" />
    <input data-val="true" data-val-date="The field Arrival Time must be a date." data-val-required="The Arrival Time field is required." id="item_StartDate" name="item.StartDate" type="hidden" value="01-Jan-16 09:00:00 AM" />
    <input data-val="true" data-val-date="The field Leave Time must be a date." data-val-required="The Leave Time field is required." id="item_EndDate" name="item.EndDate" type="hidden" value="01-Jan-16 12:00:00 PM" />
    <input id="item_Day" name="item.Day" type="hidden" value="SUN" />
</td>

视图模型是:

public class GetAppointmentViewModel
{
    public string DoctorName { set; get; }
    public string DoctorId { set; get; }
    public int DepartmentId { set; get; }
    public int ScheduleId { set; get; }
    public DateTime StartDate { set; get; }
    public DateTime EndDate { set; get; }
    public string Day { set; get; }
}

这是控制器:

[HttpPost]
public async Task<ActionResult> GetAppointment(GetAppointmentViewModel model)
{
    // Some Validation which will be implemented in next iteration.
    if (ModelState.IsValid)
    {
        var appointment = new Appointment
        {
            Date = model.StartDate,
            Time = model.StartDate.TimeOfDay,
            PatientId = 1,
            DoctorId = 1
        };

        db.Entry(appointment).State = EntityState.Added;
        await db.SaveChangesAsync();

        return View(appointment);
    }

    return View();
}

我使用静态数据,因为我从ViewModel获取值。所以为了进一步发展,我只使用了静态数据。

但我想从每个隐藏的输入中省略item.

我是否必须创建自定义HiddenFor或者有办法执行此操作 在此先感谢您的帮助。

0 个答案:

没有答案