2017-03-22 28 views
0

私はいくつかのJavaScriptを保持するデータベースを持っています。ユーザーに表示されるテキストは、セクションごとに異なります。必要な正しいスクリプトがSPROCを使用して取得されます。これはコントローラ内でViewBagに割り当てられます。それがビューに表示されます。すべてのものが1つの障害物を除いて素晴らしい作品です、それは私のJavascriptの周りにjQuery関数を追加しています。私はコントローラにブレークポイントを設定し、コントローラ上にjQuery関数がないことを明確に見ることができます。私はおそらく私がスクリプトの タグをコントローラに追加して、それを修正するかもしれないがHope.Rawを使ってビューに書きましたが、それは修正されないかもしれません。ここASP.NET MVCビューで動的に生成されるjavascript

はここ

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) 

、ここでのコードは、これはjQueryのではありません

<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> 

を返されているものであり、コントローラのコードですだからそれはないはずです

(function($){ 
})(jQuery); 

これを正しく表示するには、何が必要ですか?

+0

を解決し、私はエドゥアルドが問題の権利を得たが、脇として、あなたが変数をあなたとViewBagを詰めるべきではないと思いますあなたの意見に欲しい。 _Layout.cshtmlのビューの範囲外の変数にアクセスできるように作成されました。ビューにアクセスさせたいプロパティを持つPersonalityViewModel.csクラスを作成し、それを初期化し、その値をコントローラに移入してビューに渡します。 – raykrow

+0

これが実際のコードであれば、ここに何か他のものがあります。あなたの関数はunescapeの後に '

0

が見つかりました。私はMVC Controls Toolkitをプロジェクトに持っています。フィルタの追加をコメントアウト

次のコード

public static class MySuperPackage { 
    public static void PreStart() { 
     MVCControlsToolkit.Core.Extensions.Register(); 
     //System.Web.Mvc.GlobalFilters.Filters.Add(new MVCControlsToolkit.ActionFilters.PlaceJavascriptAttribute()); 
    } 
} 

がありましたが、問題に

関連する問題