我希望在加载View页面时从同一个ActionResult方法返回部分视图和base64字符串。
这是我的视图:Index.cshtml
<body>
<div id="topimage">
@Html.Action("Fetch", "image", new { parameter = 1 }) //Seperate partial view to display top image.
<\div>
<div id="Bottomimage">
<img src=""/>//No partial View to display Bottom image here and how to call my controller method here with passing parameter.
<\div>
</body>
部分视图: _TopImage.cshtml
@model MyMvc.Demo.Model.ImageAttributes
<div>
<img src="data:image;base64,@System.Convert.ToBase64String(Model.Image)" />
</div>
我的控制器方法,它将根据参数1和0 显示顶部和底部图像:
public ActionResult Fetch(int parameter)
{
using (var db = new MyDBContext())
{
if(parameter==1)//display top image
var Image = db.ImageAttributes.Where(r => r.DisplayDirection == parameter).FirstOrDefault();
return PartialView("_TopImage", Image );
else
{
var Image = db.ImageAttributes.Where(r => r.DisplayDirection == parameter).FirstOrDefault();
//Now here i just want to return Bytes of image which i will get in my ad Object as
//becasue there is no seperate partial view to display bottom image.so i will
//direclty return bytes from here and convert in to base 64 sstring there to render bottom image
//for eg: <img src="data:image;base64,@System.Convert.ToBase64String(Model.Image)" />
//How to do this
}
}
}
注意:我不想创建Seperate Controller方法,我不想改变我的任何视图。
任何人都可以指导我这个???
答案 0 :(得分:1)
如果你不想两次调用动作,你可以将动作返回HtmlString存储在一个变量中:
@{
var Image = Html.Action("Fetch", "image", new { parameter = 1 });
}
<div id="topimage">
@Image //Seperate partial view to display top image.
<\div>
<div id="Bottomimage">
@image
<\div>
在这种情况下修改你的行动其他部分如下:
else
{
var Image = db.ImageAttributes.Where(r => r.DisplayDirection == parameter).FirstOrDefault();
var base64Image = Convert.ToBase64String(Image.Image);
var byteArray = Convert.FromBase64String(base64Image);
return File(byteArray , "image/png", "image.png");
}
并在视图中:
<img src="@Url.Action("Fetch", "image", new { parameter = 0 })" />
答案 1 :(得分:0)
如果您想将部分视图加载到其所有视图页面,只需从您的视图的共享文件夹中调用您的部分视图