I'm wondering what's the best strategy to delete a row in MVCContrib generated grid.
Consider this grid:
Html.Grid(Model.Proc.Documents).Columns(column =>
{
column.For(c => c.Name).Named("Title");
column.For(c => c.Author.Name).Named("Author");
column.For("Action").Action(
delegate(DocumentDto doc)
{
const string cbxFrmt =
"<td><strong><a href=\"#\" onclick=\"DocDetails({0})\">View details</a></strong> | " +
"<strong><a href=\"#\" onclick=\"RemoveDoc({1})\">Delete</a></strong>" +
"</td>";
Response.Write(string.Format(cbxFrmt, doc.Id, doc.Id));
}
).DoNotEncode().Named("Action");
})
.Attributes(id => "documentgrid"));
Each row has a link which calls RemoveDoc(docid) javascript function, where I should remove the row by calling an action in controller to actually remove the document in data model, which is quite easy, but then I can't figure out how to remove the row from tbody with Jquery. Please advise. Am I on a complitely wrong track ? What's the best way to do it ?
And about adding a row. Initially I thought of doing it somehow like that:
function UploadDocument() {
var action = '<%=Html.BuildUrlFromExpression<MyController>(c => c.UploadDocument(parameters))%>';
$.ajax({
type: "POST",
url: action,
data: { data to upload },
cache: false,
dataType: 'text',
success: function (data, textStatus) {
var newRow = "<tr class='gridrow'><td>" + v1 +
"</td><td>" + doc_author + "</td>" +
"<td><strong><a href=\"#\" onclick=\"DocDetails(doc_id_returned_by_ajax)\">View details</a></strong> | " +
"<strong><a href=\"#\" onclick=\"RemoveDoc(doc_id_returned_by_ajax)\">Delete</a></strong>" +
"</td>" +
"</tr>";
var docgrid = $('#documentgrid');
var tbody = $("table[class='grid']>tbody");
$(tbody).append(newRow);
},
error: function (xhr, textStatus, errorThrown) {
var msg = JSON.parse(xhr.responseText);
alert('Error:' + msg.Message);
}
});
}
But I'm not sure this is the best practise to do it. Any other strategies ?
Thanks!
Maybe something among the lines:
and in
TableLinks.ascx
:and in the controller:
As you can see I use a standard form with a submit button to delete. If you want to use AJAX it is very easy to generate a simple link and attach to the click event with jquery as you did in your example:
and finally progressively enhance the link:
You can see those concepts in action in this sample project.