我制作了一个关于日历的自定义控制器。但是一个div需要附加到身体上。我搜索,但一无所获。我实现的以下代码有效,但不是很好。我知道,div应该放在body的末尾,就像jquery $('body').append('div')
但我不知道如何在MVC帮助器中编码。
public static MvcHtmlString CalendarTextBox(this HtmlHelper helper, string name, DateTime? value, DateTime start, DateTime max, string nextTriggerId, object htmlAttributes = null)
{
var result = helper.TextBox(name, value, htmlAttributes);
var str = result.ToHtmlString().Replace(@"/>", string.Format(@" mod=""calendar|notice""
mod_notice_tip=""yyyy-mm-dd""
mod_calendar_rangeStart=""{0}""
mod_calendar_rangeEnd=""{1}""
mod_calendar_focusNext=""{2}"" //>", start.ToString("yyyy-MM-dd"), max.ToString("yyyy-MM-dd"), nextTriggerId));
if (null == helper.ViewData["calendar"])
{
str += string.Format(
@"<div style=""HEIGHT: 0px""id=""jsContainer""class=""jsContainer""><div style=""DISPLAY: none""id=""jsHistoryDiv""></div><textarea style=""DISPLAY: none""id=""jsSaveStatus""></textarea><div style=""Z-INDEX: 999; POSITION: absolute; DISPLAY: none; OVERFLOW: hidden""
id=""tuna_alert""></div><div style=""Z-INDEX: 120; POSITION: absolute; VISIBILITY: hidden""
id=""tuna_jmpinfo""></div><div style=""Z-INDEX: 120; POSITION: absolute; DISPLAY: none""id=""tuna_calendar""
class=""tuna_calendar""></div></div><SCRIPT type=text/javascript charset=utf-8 src=""{0}/js/datepicker/tuna_100324_jsLoader.js""></script><script type=""text/javascript"">var JL=new CtripJsLoader();var files=[[""{0}/js/datepicker/tuna_100324_index.js"",""utf-8"",true]];JL.scriptAll(files);</script>",
Define.ASSET_PATH);
helper.ViewData["calendar"] = 1;
}
return MvcHtmlString.Create(str);
}
答案 0 :(得分:1)
将$('body').append('div');
行注入生成的HTML并将语句嵌套在文档就绪函数的javascript块中:
// ...
str += string.Format(
@"<script type=""text/javascript"">$(document).ready(function () {
$('body').append('div'); } </script>");
helper.ViewData["calendar"] = 1;
// ...