gpt4 book ai didi

javascript - 如何在不继承Controller的类中创建actionlink?

转载 作者:太空宇宙 更新时间:2023-11-04 15:27:55 25 4
gpt4 key购买 nike

我正在开发一个 C# mvc 应用程序,我在我的应用程序中使用这个 javascript 日历:http://fullcalendar.io/ 。我展示的事件是法庭听证会。以下是我所服务的方法的一部分,用于构建法庭听证会的陈述:

var obj = new
{
id = hearing.id,
title = string.Format("Hearing ({0} - {1}) in the case №{2}/{3}", hearing.HearingStart.Value.ToString("H:mm"), hearing.HearingEnd.Value.ToString("H:mm"), hearing.CaseNumber, hearing.CaseYear),
start = hearing.HearingStart.Value.ToString("yyyy-MM-dd HH:mm:ss"),
end = hearing.HearingEnd.Value.ToString("yyyy-MM-dd HH:mm:ss")
};

在该方法的这一行:

title = string.Format("Hearing ({0} - {1}) in the case №{2}/{3}", hearing.HearingStart.Value.ToString("H:mm"), hearing.HearingEnd.Value.ToString("H:mm"), hearing.CaseNumber, hearing.CaseYear)

我想让 caseNumber 成为指向此站点的 ActionLink:http://93.152.175.226/Cases/Details/327688

我在 google 和 stackoverflow.com 中查找了示例,这些示例显示了在 c# 代码中使用 ActionLink 的正确语法(不在 View 中,而是在服务方法中),但没有找到任何示例。

这是 Controller 中的方法:

public ActionResult GetHearingsForJudgePanel(string id, int? month = null, int? year = null, int? caseNumber = null, int? caseYear = null)
{
if (!Request.IsAjaxRequest())
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

int currentMonth;
int currentYear;
DateTime currentDate = DateTime.Now;
EventService service = new EventService();
IEnumerable<object> data;

if (id == "")
{
id = null;
}

if (month.HasValue)
currentMonth = month.Value;
else
currentMonth = currentDate.Month;

if (year.HasValue)
currentYear = year.Value;
else
currentYear = currentDate.Year;

data = service.GetHearingsForJudgePanel(id, currentMonth, currentYear, caseNumber, caseYear);

return Json(data, JsonRequestBehavior.AllowGet);
}

这是我的服务方法: public IEnumerable GetHearingsForJudgePanel(string id, int 月份, int 年, int? caseNumber, int? caseYear) { 列表结果 = new List(); 列表听证会 = new List();

        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["DapperConnection"].ToString();

con = new SqlConnection(constr);

hearings = con.Query<HearingsForCalendarViewModel>("GetHearingsForCalendar", new { id, month, year, caseNumber, caseYear }, commandType: CommandType.StoredProcedure).ToList();

const string protocol = "http://";
const string baseAddress = "93.152.175.226";
const string path = "/Cases/Details/";

foreach (var hearing in hearings)
{
var link = $"<a href=\"{protocol}{baseAddress}{path}{hearing.caseId}\">{he‌​aring.CaseNumber}</a‌​>";
var obj = new
{
id = hearing.id,
title = string.Format("Hearing({0} - {1}) in the case №{2}/{3}", hearing.HearingStart.Value.ToString("H:mm"), hearing.HearingEnd.Value.ToString("H:mm"), link, hearing.CaseYear),

start = hearing.HearingStart.Value.ToString("yyyy-MM-dd HH:mm:ss"),
end = hearing.HearingEnd.Value.ToString("yyyy-MM-dd HH:mm:ss")
};

result.Add(obj);

}

return result;
}

最佳答案

在 MVC Controller 中,您可以通过基类 System.Web.Mvc.ControllerUrl 属性访问 UrlHelper :

var link = Url.Action("Details", "Cases", new { id = caseNumber });

如果链接应该构建在不继承自 System.Web.Mvc.Controller 的类中(例如 ViewModel 或 Service),您可以创建一个 UrlHelper code> 通过传递当前的 RequestContext:

var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
var link = urlHelper.Action("Details", "Cases", new { id = caseNumber });

或者将 UrlHelper 从 Controller 注入(inject)到服务方法中。

关于javascript - 如何在不继承Controller的类中创建actionlink?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45073471/

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