Jquery 在asp.net MVC 5中使用部分视图时出错
我正在尝试使用asp.net MVC 5中的部分视图。以下是我遵循的步骤。 1) 在asp.net MVC应用程序中定义控制器和模型 模型详情如下:Jquery 在asp.net MVC 5中使用部分视图时出错,jquery,asp.net-mvc,c#-4.0,Jquery,Asp.net Mvc,C# 4.0,我正在尝试使用asp.net MVC 5中的部分视图。以下是我遵循的步骤。 1) 在asp.net MVC应用程序中定义控制器和模型 模型详情如下: public class ImageData { public int id { get; set; } public string imagePath { get; set; } public string encodedImage { get; set; } } 控制器具有如下方法 public class Pop
public class ImageData
{
public int id { get; set; }
public string imagePath { get; set; }
public string encodedImage { get; set; }
}
控制器具有如下方法
public class PopupWindowController : Controller
{
// GET: PopupWindow
public ActionResult Index()
{
ImageData objImg = new ImageData();
return View(objImg);
}
[HttpPost]
public ActionResult PopupwindowDisplay(ImageData objImg)
{
ClsDataOperations objDO = new ClsDataOperations();
//ImageData objImg = new ImageData();
objImg = objDO.GetImageData();
Byte[] inputBytes = System.IO.File.ReadAllBytes(objImg.imagePath);
Byte[] outputBytes;
using (System.IO.MemoryStream inStream = new MemoryStream(inputBytes))
using (MemoryStream outStream = new MemoryStream())
{
System.Drawing.Bitmap.FromStream(inStream).Save(outStream, System.Drawing.Imaging.ImageFormat.Png);
outputBytes = outStream.ToArray();
}
objImg.encodedImage = Convert.ToBase64String(outputBytes);
bool modelState = ModelState.IsValid;
return PartialView(objImg);
}
}
<a href="javascript:;" id="openwindow" class="btn btn-primary">Open Window</a>
将局部视图定义为“PopupUpindowDisplay.cshtml”
@model orderestagingtest.DataAccess.ImageData
模式弹出窗口的测试
映像路径为:@Model.imagePath.ToString()
我试图从中加载此部分视图的cshtml主页面如下所示
@model OrderRestagingTest.DataAccess.ImageData
@{
ViewBag.Title = "Display the PopUp Window from View";
}
<h2>Displaying the Popup Window on click on Button</h2>
<br/>
<p>
<a href="" id="openwindow" class="btn btn-primary">Open Window</a>
</p>
<div id="testview" style="width:50%; height:130px; display:none;">
</div>
<div id="opendialog" style="display: none">
</div>
@section scripts{
<script type="text/javascript">
$(document).ready(function () {
$("#opendialog").dialog({
autoOpen: false,
modal: true,
title: "View Document"
});
$("#openwindow").click(function () {
alert('button clicked');
$.ajax({
type: "POST",
url: '@Url.Action("PopupwindowDisplay", "PopupWindow")',
contentType: "application/html; charset=utf-8",
dataType: "html",
success: function (result)
{
alert(result);
$("#testview").html(result);
},
failure: function (result) {
alert("failure",+result);
},
error: function (result) {
console.log(result);
alert("Error" + result);
}
});
});
});
</script>
}
@model orderestagingtest.DataAccess.ImageData
@{
ViewBag.Title=“从视图显示弹出窗口”;
}
在单击按钮时显示弹出窗口
@节脚本{
$(文档).ready(函数(){
$(“#opendialog”).dialog({
自动打开:错误,
莫代尔:是的,
标题:“查看文档”
});
$(“#打开窗口”)。单击(函数(){
警报(“点击按钮”);
$.ajax({
类型:“POST”,
url:'@url.Action(“PopupwindowDisplay”、“PopupWindow”),
contentType:“应用程序/html;字符集=utf-8”,
数据类型:“html”,
成功:功能(结果)
{
警报(结果);
$(“#testview”).html(结果);
},
失败:功能(结果){
警报(“故障”、+结果);
},
错误:函数(结果){
控制台日志(结果);
警报(“错误”+结果);
}
});
});
});
}
尝试在两个div中加载局部视图,但无法执行此操作。它没有;不要显示任何内容。请让我知道如何找到相同的错误
感谢你在这方面的帮助 您需要在return语句中添加视图名称
[HttpPost]
public ActionResult PopupwindowDisplay(ImageData objImg)
{
ClsDataOperations objDO = new ClsDataOperations();
//ImageData objImg = new ImageData();
objImg = objDO.GetImageData();
Byte[] inputBytes = System.IO.File.ReadAllBytes(objImg.imagePath);
Byte[] outputBytes;
using (System.IO.MemoryStream inStream = new MemoryStream(inputBytes))
using (MemoryStream outStream = new MemoryStream())
{
System.Drawing.Bitmap.FromStream(inStream).Save(outStream, System.Drawing.Imaging.ImageFormat.Png);
outputBytes = outStream.ToArray();
}
objImg.encodedImage = Convert.ToBase64String(outputBytes);
bool modelState = ModelState.IsValid;
return PartialView("_YourPartialViewName",objImg);
}
您需要在return语句中添加视图名称
[HttpPost]
public ActionResult PopupwindowDisplay(ImageData objImg)
{
ClsDataOperations objDO = new ClsDataOperations();
//ImageData objImg = new ImageData();
objImg = objDO.GetImageData();
Byte[] inputBytes = System.IO.File.ReadAllBytes(objImg.imagePath);
Byte[] outputBytes;
using (System.IO.MemoryStream inStream = new MemoryStream(inputBytes))
using (MemoryStream outStream = new MemoryStream())
{
System.Drawing.Bitmap.FromStream(inStream).Save(outStream, System.Drawing.Imaging.ImageFormat.Png);
outputBytes = outStream.ToArray();
}
objImg.encodedImage = Convert.ToBase64String(outputBytes);
bool modelState = ModelState.IsValid;
return PartialView("_YourPartialViewName",objImg);
}
简单点
$("#openwindow").click(function () {
alert('button clicked');
$.ajax({
type: "POST",
url: '@Url.Action("PopupwindowDisplay", "PopupWindow")',
success: function (result)
{
alert(result);
$("#testview").html(result);
},
failure: function (result) {
alert("failure",+result);
},
error: function (result) {
console.log(result);
alert("Error" + result);
}
});
});
我认为您不需要contentType和dataType。试试看,它会有用的。简单一点
$("#openwindow").click(function () {
alert('button clicked');
$.ajax({
type: "POST",
url: '@Url.Action("PopupwindowDisplay", "PopupWindow")',
success: function (result)
{
alert(result);
$("#testview").html(result);
},
failure: function (result) {
alert("failure",+result);
},
error: function (result) {
console.log(result);
alert("Error" + result);
}
});
});
我认为您不需要contentType和dataType。试试看,它会有用的。谢谢大家的帮助。我能够解决这个问题,问题在于 使用锚定标签
<a href="" id="openwindow" class="btn btn-primary">Open Window</a>
由于href被称为空字符串,我只是将其更改为href=“javascript:;”如下所示
public class PopupWindowController : Controller
{
// GET: PopupWindow
public ActionResult Index()
{
ImageData objImg = new ImageData();
return View(objImg);
}
[HttpPost]
public ActionResult PopupwindowDisplay(ImageData objImg)
{
ClsDataOperations objDO = new ClsDataOperations();
//ImageData objImg = new ImageData();
objImg = objDO.GetImageData();
Byte[] inputBytes = System.IO.File.ReadAllBytes(objImg.imagePath);
Byte[] outputBytes;
using (System.IO.MemoryStream inStream = new MemoryStream(inputBytes))
using (MemoryStream outStream = new MemoryStream())
{
System.Drawing.Bitmap.FromStream(inStream).Save(outStream, System.Drawing.Imaging.ImageFormat.Png);
outputBytes = outStream.ToArray();
}
objImg.encodedImage = Convert.ToBase64String(outputBytes);
bool modelState = ModelState.IsValid;
return PartialView(objImg);
}
}
<a href="javascript:;" id="openwindow" class="btn btn-primary">Open Window</a>
谢谢大家的帮助。我能够解决这个问题,问题在于 使用锚定标签
<a href="" id="openwindow" class="btn btn-primary">Open Window</a>
由于href被称为空字符串,我只是将其更改为href=“javascript:;”如下所示
public class PopupWindowController : Controller
{
// GET: PopupWindow
public ActionResult Index()
{
ImageData objImg = new ImageData();
return View(objImg);
}
[HttpPost]
public ActionResult PopupwindowDisplay(ImageData objImg)
{
ClsDataOperations objDO = new ClsDataOperations();
//ImageData objImg = new ImageData();
objImg = objDO.GetImageData();
Byte[] inputBytes = System.IO.File.ReadAllBytes(objImg.imagePath);
Byte[] outputBytes;
using (System.IO.MemoryStream inStream = new MemoryStream(inputBytes))
using (MemoryStream outStream = new MemoryStream())
{
System.Drawing.Bitmap.FromStream(inStream).Save(outStream, System.Drawing.Imaging.ImageFormat.Png);
outputBytes = outStream.ToArray();
}
objImg.encodedImage = Convert.ToBase64String(outputBytes);
bool modelState = ModelState.IsValid;
return PartialView(objImg);
}
}
<a href="javascript:;" id="openwindow" class="btn btn-primary">Open Window</a>
您是否在成功回拨中获得了一些数据我将警报放在成功方法中,但没有收到任何警报消息。但在调试时的控制器方法PopupUpIndowDisplay中,我可以看到objImg具有imagePath和encodedimage的值。在我提到@Model.imagePath.ToString()的局部视图中也出现了同样的情况。不知道为什么它不会成功尝试类似这样的东西returnpartialview(“testview”,objImg)代码>查看上面的链接是否对您有所帮助。您是否在成功回拨中获取了一些数据我将警报放在成功方法中,但没有收到任何警报消息。但在调试时的控制器方法PopupUpIndowDisplay中,我可以看到objImg具有imagePath和encodedimage的值。在我提到@Model.imagePath.ToString()的局部视图中也出现了同样的情况。不知道为什么它不会成功尝试类似这样的东西returnpartialview(“testview”,objImg)代码>查看上述链接是否对您有帮助。如果您的viewname与actionname相同,则不需要查看名称如果您的viewname与actionname相同,则不需要查看名称