gpt4 book ai didi

asp.net - 未调用 ActionResult

转载 作者:行者123 更新时间:2023-12-02 04:46:00 27 4
gpt4 key购买 nike

我有一个 View ,我正在其中调用 ActionResult 方法,但在该方法中放置一个断点告诉我它没有被调用。

<div>
<ul class="list-group">
@foreach (var item in Model)
{
<li class="list-group-item">
<h4>Slide ID: @item.SlideId</h4>
<p><i>Received: @item.TimeStamp</i></p>
<div class="row">
<div class="col-md-4">
<h4>@Html.ActionLink("View details", "Well", new {slideid = item.SlideId})</h4>
<img src="@Url.Action("Index", "Images", new {id = item.SlideId})"/> //This is where I want to call the method
</div>
</div>
</li>
}
</ul>

方法如下:

public class ImagesController : Controller
{
// GET: Images
public ActionResult Index(string id)
{
byte[] imageData = new byte[0];
string cs = "Data Source=" + "some path";

using (SQLiteConnection con = new SQLiteConnection(cs))
{
string stm = "SELECT LastImage FROM Well WHERE SlideId = " + "'" + id + "'";
con.Open();

using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
{
using (SQLiteDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
imageData = Serialize(rdr["LastImage"]);
}

rdr.Close();

}
}

con.Close();
}
return File(imageData, "image/png");
}

public static byte[] Serialize(object obj)
{
var binaryFormatter = new BinaryFormatter();
var ms = new MemoryStream();
binaryFormatter.Serialize(ms, obj);
return ms.ToArray();
}
}

我试图用这段代码实现的是将图像从数据库加载到 View 中。关于我做错了什么的任何提示?

现在有了 RouteConfig:

public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}

最佳答案

当您编写 <img src="@Url.Action("Index", "Images", new {id = item.SlideId})"/> 时,您调用的不是操作而是路由 url。结果是一个字符串,例如 localhost:8080/images/index/abcd123456 所以,如果你想调用 Action ,你需要使用 @Html.Action("Index", "Images", new {id = item.SlideId}) 。注意 @Html.Action 而不是 @Url.Action

关于asp.net - 未调用 ActionResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32823830/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com