I have a form which can have multiple values in it.
For example:
Please list your dependants.
I have a fieldset containing my three fields
- First name,
- Last name,
- Date of Birth
as well as an Add button.
My add button fires of a jQuery ajax request and upon the success adds a table row into the table
Pseudo Code:
$.ajax({
url: myUrl,
data: myData,
success: function(){
var row = $("<tr id='...'>");
row.append("<td>").text(txtFirstName.val());
row.append("<td>").text(txtLastName.val());
row.append("<td>").text(dteDateOfBirth.val());
row.appendTo($("#myDependantsTable"));
}
...
});
This works great. However, now I have my HTML defined in two different places, my javascript and also in my View
IE:
<tr>
<td>@Model.FirstName</td>
<td>@Model.LastName</td>
<td>@Model.DateOfBirth</td>
</tr>
A problem comes into play when I start modifying these, such as, adding a class on the date column, I need to modify the code twice.
Is there anyway around this problem?
I would like:
- Rows to be added "ajaxy" without a postback
- Consistent layout where I only need to make a change in one place