vendredi 8 avril 2016

form.searialize does not post few hidden fields

I have below view model:

public class TasksViewModel
{
    public TasksViewModel()
    {
         Status = new SelectList(Enumerable.Empty<SelectListItem>());
         TaskID = 0;
         TaskDate = DateTime.Today;
         StatusID = "1";
    }

    public int TaskID { get; set; }

    [Required(ErrorMessage = "Date is required")]
    public DateTime TaskDate { get; set; }

    public string AssignedBy { get; set; }

    [StringLength(1000, ErrorMessage = "Maximum limit reached")]
    [Required(ErrorMessage = "Description is required")]
    public string TaskDescription { get; set; }

    [Required(ErrorMessage = "Please select an user")]
    public int UserId { get; set; }
    public SelectList Users { get; set; }
    public string UserName { get; set; }

    [StringLength(1000,ErrorMessage="Maximum limit reached")]
    public string AdditionalNotes { get; set; }

    [Required(ErrorMessage = "Please select a status")]
    public string StatusID { get; set; }
    public SelectList Status { get; set; }
    public string StatusStr { get; set; }
}

and have below view for editing

@using (Html.BeginForm("AddEditUser", "Admin", FormMethod.Post, new { id = "frmNotes"}))
{
     @Html.AntiForgeryToken();
     <div style="float:right">
          @Html.DropDownListFor(m => tasks.StatusID, tasks.Status, "Please select a Status", new { @class = "selectpicker" })
          @Html.ValidationMessageFor(m => tasks.StatusID)
     </div>
     @Html.HiddenFor(m => tasks.TaskID);
     @Html.HiddenFor(m => tasks.TaskDate);
     @Html.HiddenFor(m => tasks.UserId);
     @Html.HiddenFor(m => tasks.TaskDescription);
     @Html.TextAreaFor(m => tasks.AdditionalNotes, htmlAttributes: new { placeholder = "Enter additional notes", @class = "form-control materialize-textarea", style = "padding:1.6rem 0; min-height:3rem", length = 1000 })
     <button data-href="@Url.Action("AddEditTask", "Admin")"><i class="material-icons">update</i></button>
}

In my view, I have multiple hidden fields for different model data which I post along with form to overcome ModelState validation and I do not want this data to be edited. But out of above 4 hidden fields only 2 gets posted i.e. tasks.TaskID and tasks.TaskDate but not other 2 i.e. tasks.UserId and tasks.TaskDescription. I use form.serialize to post the values on button click as below:

var _form = _elem.closest('form');
if (_form.valid()) {
     var formdata = _form.serialize();
     var request = $.post(_url, formdata);
     request.complete(function (response) {
           if (response.responseJSON.result) {
               Materialize.toast(response.message, 5000, 'rounded');
           } else {
               Materialize.toast(response.message, 5000, 'rounded')
           }
     }).error(function () {
           Materialize.toast("Something went wrong on our side!", 5000, 'rounded')
     })
}

and here is my controller signature

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddEditTask(TasksViewModel m)

When I do a quick watch on m above, this is what it looks like.

Test Image

I did a console.log on formdata and I can see the values there..

__RequestVerificationToken=XdsadgBZ657qNyDbq6KPadasdaye9u4f9wAX0JLtbhfuYTbykensxVCm_zSzevnUCfE6t2sXSYbhoLekHZ8damvfFOCPGRGWCo98adas7da7dhqBY4tjNt2i4JUds3s8dxwvRQN6BoyvSv1Ed454dada9KaknOpz0oTNNnfHqadGajszGh-_rXQuNe4k1&tasks.StatusID=2&tasks.TaskID=1003&tasks.TaskDate=4%2F8%2F2016+12%3A00%3A00+AM&tasks.UserId=1002&tasks.TaskDescription=Test+Task+2&tasks.AdditionalNotes=Test

and as you can see all the hidden fields are included.. Where this will be lost.. Why this will not be received in controller parameter? Any helps or insights appreciated.

Aucun commentaire:

Enregistrer un commentaire