Hi bigbear,
Check this example. Now please take its reference and correct your code.
Model
public class CustomerModel
{
[Required(ErrorMessage = "Name is required.")]
public string Name { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View(new CustomerModel());
}
[ValidateAntiForgeryToken]
[HttpPost]
[HandleError]
public ActionResult UpdateServiceTables(CustomerModel model)
{
return PartialView("_SearchCustomer", model);
}
public JsonResult GetServiceQuote(int? id)
{
if (id == null) { return null; }
var vm = new CustomerModel();
vm.Name = "Test";
return Json(vm, JsonRequestBehavior.AllowGet);
}
}
View
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('.openModal').on('click', function () {
var quoteid = $(this).attr("data-quoteid");
$.ajax({
type: "GET",
url: "/Home/GetServiceQuote/",
contentType: "application/json; charset=utf-8",
data: { "id": quoteid },
datatype: "Json",
success: function (data) {
var modalA = $('#modalInnerSQ');
var body = $('.modal-body');
var quoteid = modalA.find(body).find('#Name');
$(quoteid).val(data.Name);
OpenServiceQuoteModal();
}
});
});
});
function OpenServiceQuoteModal() {
$('#modalInnerSQ').modal('show');
}
</script>
<input type="button" class="openModal btn btn-primary" data-quoteid="1" value="Open" />
<%using (Html.BeginForm("UpdateServiceTables", "Home", FormMethod.Post, new { model = Model }))
{%>
<div class="modal fade" id="modalInnerSQ" tabindex="-1" role="dialog" aria-labelledby="lblSQmodal"
aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">
<b>Service Quote Details</b></h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="modalbody">
<%:Html.Partial("_SearchCustomer")%>
</div>
<div class="modal-footer">
<input type="submit" name="submit" class="btn btn-success pull-right" value="Ok" />
</div>
</div>
</div>
</div>
<% } %>
PartialView
<%:Html.AntiForgeryToken() %>
<%:Html.ValidationSummary(true, "", new { @class = "text-danger" })%>
<table>
<tr>
<td>
Name
</td>
<td>
<%:Html.TextBoxFor(model => model.Name)%>
<%:Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })%>
</td>
</tr>
</table>
Screenshot