ASP.NET MVC视图中动态生成的javascript
我有一个数据库,里面有一些Java脚本。显示给用户的文本因章节和语言而异。使用存储过程提取所需的正确脚本。然后在控制器中将其分配给ViewBag。然后,它将显示在视图上。除了一个障碍外,每件事情都很好,那就是在我的Javascript周围添加jQuery函数,这使得它无法工作。我在控制器中设置了一个断点,可以清楚地看到它在控制器上没有jQuery函数。我想如果我加上剧本 标记,然后使用Html.Raw将其写入视图,这样它可能会修复它,但不会 这是控制器中的代码ASP.NET MVC视图中动态生成的javascript,javascript,c#,asp.net-mvc,Javascript,C#,Asp.net Mvc,我有一个数据库,里面有一些Java脚本。显示给用户的文本因章节和语言而异。使用存储过程提取所需的正确脚本。然后在控制器中将其分配给ViewBag。然后,它将显示在视图上。除了一个障碍外,每件事情都很好,那就是在我的Javascript周围添加jQuery函数,这使得它无法工作。我在控制器中设置了一个断点,可以清楚地看到它在控制器上没有jQuery函数。我想如果我加上剧本 标记,然后使用Html.Raw将其写入视图,这样它可能会修复它,但不会 这是控制器中的代码 public ActionRes
public ActionResult Personality(int AssessmentID, int ProfileID, int LanguageID)
{
string script = "<script type=\"text/javascript\">";
var questions = db.CRI_AssessmentQuestions.Where(a => a.SectionID == 1 && a.LanguageID == LanguageID).OrderBy(a => a.Sequence);
using (SqlConnection oConn = new SqlConnection(ConfigurationManager.ConnectionStrings["CRI_Support"].ConnectionString))
{
SqlCommand oCmd = new SqlCommand();
object pages;
oCmd.CommandText = "CRI_GetSectionCount";
oCmd.CommandType = CommandType.StoredProcedure;
oCmd.Parameters.Add(new SqlParameter("@AssessmentID", AssessmentID));
oCmd.Connection = oConn;
oConn.Open();
pages = oCmd.ExecuteScalar();
oConn.Close();
oCmd.Dispose();
ViewBag.NumPages = pages.ToString();
SqlCommand myCmd = new SqlCommand();
object mids;
myCmd.CommandText = "CRI_GetAllowedMiddles";
myCmd.CommandType = CommandType.StoredProcedure;
myCmd.Parameters.Add(new SqlParameter("@AssessmentID", AssessmentID));
myCmd.Connection = oConn;
oConn.Open();
mids = myCmd.ExecuteScalar();
oConn.Close();
myCmd.Dispose();
ViewBag.Middles = mids.ToString();
SqlCommand tCmd = new SqlCommand();
object totalQ;
tCmd.CommandText = "PersonalityQuestionCount";
tCmd.CommandType = CommandType.StoredProcedure;
tCmd.Parameters.Add(new SqlParameter("@AssessmentID", AssessmentID));
tCmd.Connection = oConn;
oConn.Open();
totalQ = tCmd.ExecuteScalar();
oConn.Close();
tCmd.Dispose();
SqlCommand cmd = new SqlCommand();
Object returnValue;
cmd.CommandText = "CRI_GetNewScript";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@SectionID", 1));
cmd.Parameters.Add(new SqlParameter("@LanguageID", LanguageID));
cmd.Connection = oConn;
oConn.Open();
returnValue = cmd.ExecuteScalar();
oConn.Close();
cmd.Dispose();
script += returnValue.ToString() + "</script>";
script = script.Replace("#iPageNumber#", "1").Replace("#iTotalPages#", pages.ToString()).Replace("#iAllowedMiddles#", mids.ToString()).Replace("#TotalQuestions#", totalQ.ToString());
}
ViewBag.Script = script;
ViewBag.AssessmentID = AssessmentID;
ViewBag.ProfileID = ProfileID;
ViewBag.LanguageID = LanguageID;
return View(questions);
}
这是要归还的东西
<script type='text/javascript' >
(function($){
var message = "";
var pagenumber = 1;
var totalpages = 6;
var middles = 7;
var boolDisplayPassword = false;
var allow = "";
switch(middles){
case 7:
allow = "twenty (20) minutes";
break;
case 6:
allow = "seventeen (17) minutes";
break;
case 5:
allow = "fourteen (14) minutes";
break;
case 4:
allow = "twelve (12) minutes";
break;
case 3:
allow = "nine (9) minutes";
break;
case 2:
allow = "six (6) minutes";
break;
}
function instructions(){
section="Personality"
self.status=": This is section "+pagenumber+" of "+totalpages
message+="<br /><br /><p>The first assessment is a series of 76 questions that is not timed. There are no right or wrong answers. ";
message+="Since we are interested in your style of doing things, it is in your best interest to answer the questions ";
message+="with openness and honesty using your own value system, not what you think the Administrator wants you to say.";
message+=" There is a consistency measure built into the assessment, so guessing at the intent of the questions is likely ";
message+="to present a highly distorted profile of your results. Your candid responses are always the best answers.</p>";
message+="<p>Answer each question by clicking the circle next to the answer you want to select.</p>";
message+="<p><strong>DO NOT SELECT MORE THAN "+middles+" MIDDLE RESPONSES!</strong></p>";
message+="<p>This exercise should take approximately " + allow +", but it is not timed. Answer each question honestly.";
message+=" If you give untrue responses, they will show up in the assessment results!</p><br /><p>Please be aware that taking this assessment when ill, under the influence ";
message+="of medications that can impact your mental alertness, or when you are feeling overly tired may negatively impact the results.";
message+=" We encourage you to take the assessment when you are rested and feeling well.</p>";
message+='<br /><br /><p style="text-align:center;"><a href="javascript:void(0)" onClick="displayAssessment();" style="font:bold 1.2em arial" alt="click to continue"><strong>continue</strong></a></p>';
document.getElementById('message').style.display = "block";
document.getElementById('message').innerHTML = message;
}
function returncheck(f){
var totalMids = 0;
var numCheckedResp = 0;
var numQuestion = 0;
var currOption = "";
for(i=0; i < f.elements.length; i++){
if(f.elements[i].type == "radio"){
if(f.elements[i].name == currOption){
if(f.elements[i].checked){
numCheckedResp++;
if(f.elements[i].value == "2"){
totalMids ++;
}
}
}else{
if(f.elements[i].checked){
numCheckedResp++;
}
currOption = f.elements[i].name;
numQuestion++;
}
}
}
if(f.MidChoices.value == 0){
f.MidChoices.value = totalMids;
}
if(!(numQuestion == numCheckedResp)){
ErrMsg = "You have answered "+numCheckedResp+" of the "+numQuestion+" questions.<br />Please go back and answer all questions.";
document.getElementById('errors').innerHTML = ErrMsg;
document.getElementById('DisplayUserError').style.visibility = 'visible';
return false;
}else{
if(totalMids > 7){
ErrMsg = "You have answered "+totalMids+" middle Questions.<br />Please go back and change "+(totalMids - 7)+" of the answers.";
document.getElementById('errors').innerHTML = ErrMsg;
document.getElementById('DisplayUserError').style.visibility = 'visible';
return false;
}else{
return true;
}
}
}
function displayAssessment(){
document.getElementById('message').innerHTML = "";
document.getElementById('message').style.display = "none";
document.getElementById('AssessmentData').style.display = "block";
}
})(jQuery);
</script>
我需要做些什么才能使它正确显示?查看
Personality.cshtml
视图。在那里你可以找到(函数($){
code我找到了原因。我在项目中有MVC控件工具包
有以下代码
public static class MySuperPackage {
public static void PreStart() {
MVCControlsToolkit.Core.Extensions.Register();
//System.Web.Mvc.GlobalFilters.Filters.Add(new MVCControlsToolkit.ActionFilters.PlaceJavascriptAttribute());
}
}
注释添加过滤器解决了这个问题我认为Eduardo正确地解决了这个问题,但作为旁白,您不应该在视图中填充所需的变量。创建视图包是为了让您能够在_Layout.cshtml的视图范围之外访问这些变量。使用您希望您的视图能够访问的属性,然后初始化它并在控制器中填充它的值,并将其传递给您的视图。如果这是实际的代码,那么这里还发生了其他事情。您的函数使用
“
”构建脚本,这将是unescape之后的
,但是您的“返回内容”显示
。请注意引号和间距的差异。很明显,这里没有显示某些内容。是的,我也看到了。
(function($){
})(jQuery);
public static class MySuperPackage {
public static void PreStart() {
MVCControlsToolkit.Core.Extensions.Register();
//System.Web.Mvc.GlobalFilters.Filters.Add(new MVCControlsToolkit.ActionFilters.PlaceJavascriptAttribute());
}
}