模态弹出窗口中的 PartialView
PartialView inside modal Popup
我正在尝试在我的 MVC、Umbraco 项目中的 link 单击事件中打开 jquery 模态弹出窗口中的 PartialView。部分视图加载正常,但是在模态弹出窗口中提交按钮会正确调用 ActionMethod,但不是在同一父 window 中打开模态弹出窗口,而是在新的 window 中启动它。不知道我在这里做错了什么。下面是我的代码:
局部视图
@inherits Umbraco.Web.Mvc.UmbracoViewPage<Source.Models.SchoolFindYouModel>
<script>
$(function () {
$("#dialog-modal-school").dialog({
autoOpen: false,
width: 650,
height: 500,
show: {
effect: "drop",
duration: 1000
},
hide: {
effect: "drop",
duration: 1000
}
});
$("#modal-opener-school").click(function () {
$("#dialog-modal-school").dialog("open");
});
});
</script>
<a href="#" id="modal-opener-school">Let Schools find you</a>
<div id="dialog-modal-school" title="Let Schools find you">
<p>Upload your CV/Resume for free.</p>
@using (Html.BeginForm("SchoolFindsYou", "School", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="editor-label">
@Html.LabelFor(m => m.email, "Email address")
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.email)
@Html.ValidationMessageFor(model => model.email)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.password, "Password")
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.password)
@Html.ValidationMessageFor(model => model.password)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.search, "Search keyword")
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.search)
@Html.ValidationMessageFor(model => model.search)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.location, "Location")
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.location)
@Html.ValidationMessageFor(model => model.location)
</div>
<input type="file" name="attachment" id="attachment" />
@Html.ValidationMessageFor(model => model.attachment)
<br />
<input type="submit" name="btnSchoolFindYou" value="Upload CV/Resume" id="btnSchool"/>
<br />
<br />
<p>By clicking activate jobs you confirm that you agree to our <a href="/independent/TandC.aspx">terms and conditions</a> and <a href="/independent/Privacy.aspx">privacy policy</a></p>
}
</div>
@if (!ViewData.ModelState.IsValid)
{
<script type="text/javascript">
$(document).ready(function () {
$("#modal-opener-school").click();
});
</script>
}
PartialView 控制器
public class SchoolController : Umbraco.Web.Mvc.SurfaceController
{
[HttpGet]
public ActionResult SchoolFindsYou()
{
SchoolFindYouModel s = new SchoolFindYouModel();
return PartialView("_PartialSchoolFindYou", s);
}
[HttpPost]
public ActionResult SchoolFindsYou(SchoolFindYouModel SchoolFindYou)
{
SchoolFindYouModel s = new SchoolFindYouModel();
if (ModelState.IsValid)
{
if (Request.Files.Count > 0)
{
var file = Request.Files[0];
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/media/cv/"), fileName);
file.SaveAs(path);
}
}
return PartialView("_PartialSchoolFindYou", s);
}
else
{
return PartialView("_PartialSchoolFindYou", s);
}
}
}
回发后,它在(而不是模态弹出窗口中的我的父视图)中打开部分视图
http://localhost:49721/umbraco/Surface/School/SchoolFindsYou
这是因为您正在制作一个 http post 并将请求重定向到局部视图。您需要 return 包含此部分视图的父视图,您的部分视图将在该父视图中呈现。
因此,您应该 return View(s);
而不是控制器中的 return PartialView("_PartialSchoolFindYou", s);
,并且在该视图的某个地方您应该
@Html.Partial("_PartialSchoolFindYou", s);
我正在尝试在我的 MVC、Umbraco 项目中的 link 单击事件中打开 jquery 模态弹出窗口中的 PartialView。部分视图加载正常,但是在模态弹出窗口中提交按钮会正确调用 ActionMethod,但不是在同一父 window 中打开模态弹出窗口,而是在新的 window 中启动它。不知道我在这里做错了什么。下面是我的代码:
局部视图
@inherits Umbraco.Web.Mvc.UmbracoViewPage<Source.Models.SchoolFindYouModel>
<script>
$(function () {
$("#dialog-modal-school").dialog({
autoOpen: false,
width: 650,
height: 500,
show: {
effect: "drop",
duration: 1000
},
hide: {
effect: "drop",
duration: 1000
}
});
$("#modal-opener-school").click(function () {
$("#dialog-modal-school").dialog("open");
});
});
</script>
<a href="#" id="modal-opener-school">Let Schools find you</a>
<div id="dialog-modal-school" title="Let Schools find you">
<p>Upload your CV/Resume for free.</p>
@using (Html.BeginForm("SchoolFindsYou", "School", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="editor-label">
@Html.LabelFor(m => m.email, "Email address")
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.email)
@Html.ValidationMessageFor(model => model.email)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.password, "Password")
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.password)
@Html.ValidationMessageFor(model => model.password)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.search, "Search keyword")
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.search)
@Html.ValidationMessageFor(model => model.search)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.location, "Location")
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.location)
@Html.ValidationMessageFor(model => model.location)
</div>
<input type="file" name="attachment" id="attachment" />
@Html.ValidationMessageFor(model => model.attachment)
<br />
<input type="submit" name="btnSchoolFindYou" value="Upload CV/Resume" id="btnSchool"/>
<br />
<br />
<p>By clicking activate jobs you confirm that you agree to our <a href="/independent/TandC.aspx">terms and conditions</a> and <a href="/independent/Privacy.aspx">privacy policy</a></p>
}
</div>
@if (!ViewData.ModelState.IsValid)
{
<script type="text/javascript">
$(document).ready(function () {
$("#modal-opener-school").click();
});
</script>
}
PartialView 控制器
public class SchoolController : Umbraco.Web.Mvc.SurfaceController
{
[HttpGet]
public ActionResult SchoolFindsYou()
{
SchoolFindYouModel s = new SchoolFindYouModel();
return PartialView("_PartialSchoolFindYou", s);
}
[HttpPost]
public ActionResult SchoolFindsYou(SchoolFindYouModel SchoolFindYou)
{
SchoolFindYouModel s = new SchoolFindYouModel();
if (ModelState.IsValid)
{
if (Request.Files.Count > 0)
{
var file = Request.Files[0];
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/media/cv/"), fileName);
file.SaveAs(path);
}
}
return PartialView("_PartialSchoolFindYou", s);
}
else
{
return PartialView("_PartialSchoolFindYou", s);
}
}
}
回发后,它在(而不是模态弹出窗口中的我的父视图)中打开部分视图
http://localhost:49721/umbraco/Surface/School/SchoolFindsYou
这是因为您正在制作一个 http post 并将请求重定向到局部视图。您需要 return 包含此部分视图的父视图,您的部分视图将在该父视图中呈现。
因此,您应该 return View(s);
而不是控制器中的 return PartialView("_PartialSchoolFindYou", s);
,并且在该视图的某个地方您应该
@Html.Partial("_PartialSchoolFindYou", s);