我尝试了不同的方法,使用了 javascript 和 ajax。如果您之前遇到过这种情况,请帮忙
我有一个视图 hfnlp.cshtml
@model IMONLP.Models.HFADMwrap
@using ADM
@{
ViewBag.Title = "HFNLP";
}
<html>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<body>
**<div id="popup">
@if (true)
{
Html.RenderPartial("popup", Model);
}
</div>**
<div id="Quadrant">
@if (@Model.flag == 1)
{
Html.RenderPartial("grid", Model);
}
</div>
</body>
</html>
我有这个 popup.cshtml,它是上面视图的部分视图。会有一个弹出窗口打开并从用户那里获取内容。它必须做这些事情。1. 获取内容,将其写入模型 2. 关闭弹出窗口 3. 将控件返回给父级并且父级不应该重新加载,如果 popup.cshtml(partial view)- [part of parent winodw reloads] reloads 我很好.
这是popup.cshtml
@model IMONLP.Models.HFADMwrap
@{
ViewBag.Title = "popup";
}
<html>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script type="text/javascript">
//pop up window
$(function () {
$('#ClickMe').click(function () {
window.open('@Url.Action("FileUpload", "HFNLP")', 'FileUpload', 'height=' + (window.screen.height - 100) + ',width=700,left=' + (window.screen.width - 250) + ',top=10,status=no,toolbar=no,resizable=yes,scrollbars=yes');
});
});
</script>
<body>
<input type="button" id="ClickMe" name="ClickMe" value="New Visit"/>
</body>
</html>
单击按钮应打开弹出窗口
最后,fileupload.cshtml 是一个弹出窗口,当点击按钮时被调用
@model IMONLP.Models.HFADMwrap
@using ADM
@{
ViewBag.Title = "FileUpload";
}
<html>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(function () {
$('#ClickMe').click(function () {
//window.opener.location.href = '@Url.Action("AfterUpload", "HFNLP", new { wrap=@Model})';*@
window.opener.location.href = window.opener.location.href;
window.close();
});
});
</script>
<body>
<br />
@using (Html.BeginForm("AfterUpload", "HFNLP"))
{
<input type="button" value="submit & View Codes" style="position: absolute; left: auto; width: auto;" id="ClickMe" name = "ClickMe"/>
@*<input type="hidden" name="a" value="@Model.Hfnlp.unstructured_text"/>*@
@if (!string.IsNullOrEmpty(Model.Hfnlp.unstructured_text))
{
@Html.TextAreaFor(m => m.Hfnlp.unstructured_text, new { name= "textBox1", @value = Model.Hfnlp.unstructured_text, style = "width: 650px; height: 400px;"})
}
else
{
@Html.TextAreaFor(m => m.Hfnlp.unstructured_text, new { style = "width: 650px; height: 400px;" })
}
}
@using (Html.BeginForm("FileUploadw", "hfNLP", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" id="file" /><br />
<input type="submit" value="Upload" name="submit" />
}
</body>
</html>
使用此代码,我可以获取弹出窗口并调用控制器的操作并关闭弹出窗口。然后,整个父页面重新加载。我丢失了传递给模型的数据。请帮助我摆脱这种情况。
** * ** * ** * **编辑* ** * ** * ** ** _
public ActionResult FileUpload(HFADMwrap wrap)
{
HFNLP hf = new HFNLP();
wrap.Hfnlp = hf;
return View("FileUpload", wrap);
}
public ActionResult FileUploadw(HFADMwrap wrap, HttpPostedFileBase file)
{
HFNLP hf = new HFNLP();
if (wrap.Hfnlp == null)
{
wrap.Hfnlp = hf;
}
file = Request.Files[0];
BinaryReader b = new BinaryReader(file.InputStream);
byte[] binData = b.ReadBytes(file.InputStream.Length.ToInt32());
string filecontent = System.Text.Encoding.UTF8.GetString(binData);
hf.unstructured_text = filecontent;
/*upload and get the file content */
if (wrap.Hfnlp.unstructured_text == null)
{
return RedirectToActionPermanent("FileUpload", "FileUpload");
}
wrap.Hfnlp = hf;
return PartialView("FileUpload", wrap);
}
我现在试过了
<div id="popup">
<input type="button" value="something" onClick="?(What should be here)"/></div>