I am working on a ASP .NET MVC project and I am rendering a list of items using Html.HiddenFor
but the problem is that when all of the items rendered I am getting the name of each hidden input with the prefix of item.
and I want to omit that item. from each of the hidden input. I tried many approached but couldn't make it.
here is the code for rendering list of items:
@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>
}
}
and here is what I'm getting for each item:
<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>
The View Model Is:
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; }
}
and here is the Controller:
[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();
}
I used static data because I'm getting the values from the ViewModel. so to move further, I just used the static data.
But I want to omit item.
from each of the hidden input.
Should I have to create a custom HiddenFor
or there is a way for doing this.
Thanks in advance for the help.