我试图创建一个局部视图,当按下一个按钮时,它将以模态的形式出现。如果有另一种方法可以更好地实现这一点,我对此持开放态度——在添加
List
到主视图。如果有办法返回一个列表并形成一个单独的实体,它可能适用于我的场景。现在我的代码在某种程度上可以工作,但是你需要推动
Add Service
按钮两次,以便模态显示,当它做了唯一的方法摆脱它是提交,X在顶部以及关闭按钮都不工作。
主视图
@model List<ServicesPosting>
<div>
<button id="addService" onclick="OpenModal()">Add Service</button>
</div>
<div id="AddServiceForm"></div>
<script type="text/javascript">
function OpenModal() {
var url = '@Url.Action("AddServiceModal", "Services")';
$('#AddServiceForm').load(url);
$('#serviceModal').modal();
}
</script>
控制器
public class ServicesController : Controller
{
// GET: Services
public ActionResult Index()
{
List<ServicesPosting> postings = DataAccess.GetAllPostings();
return View(postings);
}
[HttpGet]
public ActionResult AddServiceModal()
{
return PartialView("~/Views/Services/AddNewService.cshtml");
}
}
局部视图
@model ServicesPosting
<!-- Modal -->
<div class="modal fade" id="serviceModal" tabindex="-1" role="dialog" aria-labelledby="serviceModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="serviceModalLabel">Add New Service</h4>
</div>
<div class="modal-body">
@using (Html.BeginForm("Index", "Services"))
{
// TODO: validate up front
<div class="row">
Service Title : @Html.TextBoxFor(x => x.ServiceTitle)
</div>
<div class="row">
ServiceDescription : @Html.TextAreaFor(x => x.ServiceDescription)
</div>
<div class="modal-footer">
<button type="button" class="btn" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save Changes</button>
</div>
}
</div>
</div>
</div>
</div>