0

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.

Khizar Iqbal
  • 435
  • 2
  • 11
  • 18
  • Just use a literal: `new { @name = "StartDate" }` – Kevin Gosse Mar 19 '16 at 11:26
  • You do not want the same name for each input (it would not bind). You want indexed `name` attributes which you cannot do with a `foreach` loop. Refer the dupe. –  Mar 19 '16 at 11:27
  • @KooKiz, Fortunately that does absolutely nothing. –  Mar 19 '16 at 11:27
  • @KooKiz I already tried this but it didn't help me. – Khizar Iqbal Mar 19 '16 at 11:47
  • @KhizarIqbal, Your code does not make sense. You can only post back one form at a time so what is the point of multiple forms. And there are no editable controls in the forms so there is no point posting anything. What is it that you really trying to do? –  Mar 19 '16 at 11:49
  • what I actually want to do is that whenever user click on the `Get Appointment` button, the only value of that row should be submit to the controller rather then the whole table and for this I want each hidden input to be of the same name. I'm editing my question. – Khizar Iqbal Mar 19 '16 at 11:53
  • 1
    But your not editing anything! Its pointless and just degrading your app by generating a whole lot of extra html (and heaven for a malicious user who can modify the values and screw up your app). You don't submit a form to a post method unless your editing data. –  Mar 19 '16 at 11:56
  • yes, you are right but I'll validate all of the data on server before further processing. and I don't want to allow user to edit the data. they just have to view the schedule and click on the button to get the appointment on the selected schedule. – Khizar Iqbal Mar 19 '16 at 12:01
  • But what does your `GetAppointment()` method do? –  Mar 19 '16 at 12:02
  • it just validates the data and if the data is correct, creates a database entity using the values submitted through form (which are not submitted properly), add it to database to scheduled an appointment for the user. – Khizar Iqbal Mar 19 '16 at 12:09
  • @KhizarIqbal, That makes no sense (you have not changed anything so the only thing that should be posted is the value of the ID property). Show the full code in your controller code so we can understand what your doing. –  Mar 19 '16 at 12:16

0 Answers0