During ASP.Net development i am often unsure weather i should be performing some functionality using an ajax request vs handling it all on the client with JavaScript. I often will want to use Jquery ajax functions in order to speed development up and improve maintainability even though i can achieve the same result without contacting the server.
Here is one example. In my MVC view i need to update a partial view in response to a button click. Here is a simplified version of my partial view
@foreach (Requestbox.Code.XmlContentField xmlControl in Model.RequestContentControls)
{
if (Model.IsInEditMode)
{
ViewContext.Writer.Write("<div class='editDiv'>
<a href='#' onclick='EditClick()'>Edit</a>
<a href='#' onclick='DeleteClick()'>Delete</a>");
}
switch (xmlControl.ControlType)
{
case "TextBoxField":
<div class="clearfix">
@Html.DevExpress().Label(settings => {
settings.Name = xmlControl.FieldName + "Label";
settings.AssociatedControlName = xmlControl.FieldName + "TextBox";
settings.Text = xmlControl.LabelText;
}).GetHtml()
<div class="input">
@Html.DevExpress().TextBox(settings => {
settings.Name = xmlControl.FieldName + "TextBox";
MvcExtensionHelpers.SetTextBoxSettings(settings);
settings.ControlStyle.CssClass += " full-width";
}).GetHtml()
</div>
</div>
break;
case "Header":
string header = string.Format("<{0}>{1}</{0}>", xmlControl.HeaderType,xmlControl.Value);
@Html.Raw(header)
break;
}
if (Model.IsInEditMode)
{
ViewContext.Writer.Write("</div>");
}
}
When the Edit button is clicked i need to wrap each of my sections in a div and add 2 anchor elements with onclick functions inside. I can do this by using an ajax post request and setting the Model with "IsInEditMode to true" here is the code i could call, which replaces the current partial view with the new one.
function EnableEditClick() {
$.post(rootDir + "Admin/EditClick",null,
function(partialViewResult){
$("#refTable").html(partialViewResult);
});
}
Here is the Action that could handle the ajax request
public ActionResult EditClick()
{
MyModel model = new MyModel();
model.IsInEditMode = true;
return PartialView("CustomRequest", model);
}
This is one solution, another would be to handle this all on the client by using jquery to select each of my elements that are needed and to insert the anchor elements directly. I would also be able to take some code out of the view. So something like this (Haven't tested this code but i hope you get the idea)
function EditClick()
{
var elements = $('.myTable .clearfix')
elemtns.append(' <a href='#' onclick='EditClick()'>Edit</a>
<a href='#' onclick='DeleteClick()'>Delete</a>');
}
I am torn on which will be better maintainability wise, as with the ajax way i don't have to write html in JavaScript pages and i think it will be clearer and more readable when revisting the code. However i am then using a unnecessary request to the server when i could it handle all on the client.
So my questions is should i always be doing everything on the client if possible even at the result of maintainability. Any feedback on my example is much appreciated as well.