I am new to MVC, I am trying to edit a row and sending edited data to a controller method via jQuery and AJAX, when I click on edit
the specific row becomes textboxes and save
ActionLink appears instead of edit
, but when I save it it gives me an exception because null is going in data
jQuery/Ajax Code:
$(document).ready(function () {
function toggleEditability() {
$(this).closest('tr')
.find('a.Edit, a.Save, .displayText, input[type=text]')
}
$('a.Edit').click(toggleEditability);
$('a.Save').click(function () {
toggleEditability.call(this);
var data = $(this).closest('tr').serialize();
alert(data);
//var url = $(this).attr('href');
var actionURL = '@Url.Action("Edit", "Holiday")';
$.ajax({
url: actionURL, // Url to send request to
data:data, // Send the data to the server
type: "POST", // Make it a POST request
dataType: "html", // Expect back HTML
success: function (html) {
$("#dialog-edit").dialog('open');
}
});
});
});
alert(data)
is showing this
chtml code:
<div id="details">
<table>
<tr>
@*<th>
@Html.Label("ID")
</th>*@
<th>
@Html.Label("Name")
</th>
<th>
@Html.Label("Description")
</th>
<th>
@Html.Label("Date")
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
if (Convert.ToDateTime(item.Holiday_date).Year.ToString() == DateTime.Now.Year.ToString())
{
<tr>
@* <td>
@Html.TextBoxFor(modelItem => item.Holiday_Id, new { style = "display: none; width:170px; height:15px" })
<div class="displaytext">
@Html.DisplayFor(modelItem => item.Holiday_Id)
</div>
</td>*@
<td>
<div class="HolidayName">
@Html.TextBoxFor(modelItem => item.Holiday_Name, new { id = "", style = "display: none; width:170px; height:15px" })
</div>
<div class="displaytext">
@Html.DisplayFor(modelItem => item.Holiday_Name)
</div>
</td>
<td>
<div class="HolidayDescription">
@Html.TextBoxFor(modelItem => item.Holiday_Description, new { id = "", style = "display: none; width:170px; height:15px" })
</div>
<div class="displaytext">
@Html.DisplayFor(modelItem => item.Holiday_Description)
</div>
</td>
<td>
<div class="HolidayDate">
@Html.TextBoxFor(modelItem => item.Holiday_date, new { id = "", style = "display: none; width:170px; height:15px" })
</div>
<div class="displaytext">
@Html.DisplayFor(modelItem => item.Holiday_date)
</div>
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Holiday_Id }, new { @class = "Edit", Href="#" })
@Html.ActionLink("Save", "Save", new { id = item.Holiday_Id}, new { @class = "Save", Href = "#", style = "display:none" } ) |
@Html.ActionLink("Delete", "Delete", new { id = item.Holiday_Id }, new { @class = "lnkDelete" })
@Html.ActionLink("Cancel", "Cancel", new { id = item.Holiday_Id}, new { @class = "Cancel", Href = "#", style = "display:none" } )
</td>
</tr>
}
}
</table>
</div>
Controller Method Code:
public ActionResult Edit(tbl_HolidayList tbl_holidaylist)
{
if (ModelState.IsValid)
{
db.Entry(tbl_holidaylist).State = EntityState.Modified;
db.SaveChanges();
TempData["Msg"] = "Data has been updated succeessfully";
return RedirectToAction("Index");
}
return PartialView(tbl_holidaylist);
}
tbl_HolidayList.cs
namespace OTESSystem.Models
{
using System;
using System.Collections.Generic;
public partial class tbl_HolidayList
{
public int Holiday_Id { get; set; }
public string Holiday_Name { get; set; }
public string Holiday_Description { get; set; }
public Nullable<System.DateTime> Holiday_date { get; set; }
}
}
Can you tell me how to get the textboxes values in data
for jQuery to save it?
exception and data coming null in method:
since serialize isn't returning anything I would recommend you grab the fields you want instead of serializing the row. change
to this in your ajax call
Jquery serialize() won't serialize the value in which element
name
attribute is not set. Make sure you have specified name attribute to all input element.You have to specify name attribute too in your razor code.
According to JQuery documentation
Checkout this fiddle
And one more thing you need to modify in your code is following line.
because only those element can be serialized which has name attribute and you have only input elements in your code.
Sounds like your
public ActionResult Edit(tbl_HolidayList tbl_holidaylist)
method is supposed to be accepting JSON and returning it back.Try something like this and walk through your code to see where you fail:
Lastly, change your AJAX success callback to see what you get back like so:
You're getting
null
foralert(data)
because you're trying to serialize the entire content of the closesttr
and it contains non-input controls as well. So try to serialize just the input controls like belowand your alert should return you the
tr
data as shown below in the image.If your AJAX posted model data returns
null
at theSave
action then an alternate would be to build your data array from the serialized data object and take only what you would require something like below (all the below code within your$('a.Save').click(function () { ...
)And now you should see the posted data at the controller's
Save
action.UPDATE 1: Based on the error noted in your comment it seems like your primary key is coming as
null
to the controller's action method. You can add a hidden input field to the markup as shown below.Try this, first modify your view to have some identifiers for the fields you need in the rows.
Then change your ajax calls data to be this:
Test the alert, and if the alert has the data, then set a break point on your controllers action method and see if the parameter is coming through filled in.