[英]Dynamically generated javascript in ASP.NET MVC view
我有一個包含一些Javascript的數據庫。 顯示給用戶的文本因部分和語言而異。 使用SPROC提取所需的正確腳本。 然后將其在控制器中分配給ViewBag。 然后將其顯示在視圖上。 除了一個障礙之外,所有事情都運行良好,它在我的Javascript周圍添加了jQuery函數,這使它不起作用。 我在控制器上設置了一個斷點,可以清楚地看到它在控制器上時沒有jQuery函數。 我想也許是如果我在控制器中添加了script標簽,然后使用Html.Raw將其寫到視圖中的,它可能會解決它但是沒有。
這是控制器中的代碼
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);
}
這是視圖中的代碼
@Html.Raw(ViewBag.Script)
這是返回的東西
<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>
這不是jQuery,所以它不應該有
(function($){
})(jQuery);
我需要怎么做才能使其正確顯示?
查看Personality.cshtml
視圖。 在這里您將找到(function($){
代碼
我找到了原因。 我在項目中有MVC控件工具包。
有以下代碼
public static class MySuperPackage {
public static void PreStart() {
MVCControlsToolkit.Core.Extensions.Register();
//System.Web.Mvc.GlobalFilters.Filters.Add(new MVCControlsToolkit.ActionFilters.PlaceJavascriptAttribute());
}
}
注釋掉添加過濾器可以解決此問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.