0

I have my values in LabelFors but when I do a postback the model has default values.

@using (Html.BeginForm("Delete", "Event", FormMethod.Post))
{
    <p>
        @Html.LabelFor(m => m.EventID, "Event ID")<br />
        @Html.LabelFor(m => m.EventID, Model.EventID.ToString())
    </p>
    <p>
        @Html.LabelFor(m => m.DayCode, "Type of Event")<br />
        @Html.LabelFor(m => m.DayCode, Model.DayCode.ToString())
    </p>
    <p>
        @Html.LabelFor(m => m.EventDate, "Date of Event")<br />
        @Html.LabelFor(m => m.EventDate, Model.EventDate.ToShortDateString())
    </p>
    <p>
        @Html.LabelFor(m => m.Subject, "Person or Interest")<br />
        @Html.LabelFor(m => m.Subject, Model.Subject)
    </p>
    <p>
        @Html.LabelFor(m => m.EventDesc, "Description")<br />
        @Html.LabelFor(m => m.EventDesc, Model.EventDesc)
    </p>

    <table>
        <tr>
            <td>
                @Html.ActionLink("Back", "Index", new { month = Model.EventDate.Month,
                                                        year = Model.EventDate.Year,
                                                        day = Model.EventDate.Day})
            </td>
            <td>            
                <input id="delete" type="submit" value="Submit Changes" />
            </td>
        </tr>
    </table>
}

        public ActionResult Delete(int eventID, int year, int month, int day)
        {
            //Find Event then return to Index
            EventModel thisEvent = EventRepository.getDayEvent(eventID, year, month, day);
            return View(thisEvent);
        }



        [HttpPost]
        public ActionResult Delete(EventModel deletedEvent) //here Model only has 0, "", and 1/1/0001
        {
            EventRepository.DeleteEvent(deletedEvent);
            return RedirectToAction("Index", new { year = deletedEvent.EventDate.Year, month = deletedEvent.EventDate.Month, day = deletedEvent.EventDate.Day });
        }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
dotnetN00b
  • 5,021
  • 13
  • 62
  • 95

4 Answers4

3

Try adding a HiddenFor for each value

@Html.HiddenFor(m => m.EventID)

This will ensure the value is posted back to the server when the form is submitted.

If you need to provide a value, I like the accepted answer provided here

Community
  • 1
  • 1
glosrob
  • 6,631
  • 4
  • 43
  • 73
3

Your model is empty because your form contains no data. Labels aren't considered to be data. They are labels for data. What you need is this:

@Html.LabelFor(m => m.Subject, "Person or Interest")<br /> 
@Html.DisplayFor(m => m.Subject)

Edit: To make that above part clearer, I should stress that glosrob is right in that you only need the EventId in order to process the delete. Everything else is just for displaying the correct information so the user can verify they are deleting the correct record. I just wanted to make the point that labels aren't considered to be form data, they are a visual indication.

There's a few things to note here though:

Firstly, your EventId shouldn't be open to modification, so it should be rendered with @Html.HiddenFor(m => m.EventId). This will render it as a hidden field which the user will be unable to see. That doesn't mean it can't be modified and you should eventually use an AntiForgeryToken to help against that.

Secondly, you don't need to specify the strings for the labels. You can do that on your viewmodel instead with data annotations:

// DisplayName is in System.ComponentModel and not System.ComponentModel.DataAnnotations;
using System.ComponentModel;  

public class EventViewModel
{
    [DisplayName("Person or Interest")]
    public string Subject { get; set; }
}

Then in the view you can just use @Html.LabelFor(m => m.Subject).

John H
  • 14,422
  • 4
  • 41
  • 74
2

OK, the problem is that your form has only labels, but NO inputs. It's inputs values are sent to server and then ASP.NET MVC binds then to model class.

Instead that (you just have labels, labels are not part of post payload)

@Html.LabelFor(m => m.EventDesc, "Description")<br />
@Html.LabelFor(m => m.EventDesc, Model.EventDesc)

You should have (textbox will be rendered <input /> html tag)

@Html.LabelFor(m => m.EventDesc, "Description")<br />
@Html.TextBox(m => m.EventDesc)

I also agree with comment by @glosrob, you don't have to show/allow edit your ID. It should be up to hidden input, so it will correctly bind to model on POST

@Html.HiddenFor(m => m.EventID)
Alexander Beletsky
  • 19,453
  • 9
  • 63
  • 86
1

It looks to me like your postback doesn't pass the item you're trying to delete. You could use HiddenFor(m => m.EventID)to pass your model's ID, or just put it in the form route values:

@using (Html.BeginForm("Delete", 
                       "Event", 
                       new { idToDelete = Model.EventID }, 
                       FormMethod.Post))

Of course, to do this, you'll have to change your action method to use only the ID.

Justin Morgan - On strike
  • 30,035
  • 12
  • 80
  • 104