我正在开发一个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的正确语法(不在视图中,但在服务方法中),但没有找到任何。
以下是控制器中的方法:
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 month,int year,int?caseNumber,int?caseYear) { List result = 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}\">{hearing.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;
}
答案 0 :(得分:1)
在MVC控制器中,您可以通过基类UrlHelper
的{{1}}属性访问Url
:
System.Web.Mvc.Controller
如果链接应该构建在不从var link = Url.Action("Details", "Cases", new { id = caseNumber });
继承的类中(例如ViewModel或Service),则可以通过传递当前的RequestContext来创建System.Web.Mvc.Controller
:
UrlHelper
或者您将UrlHelper从Controller注入服务方法。