簡體   English   中英

MVC和Ajax在Razor視圖中注入模型

[英]MVC and Ajax injected Models in Razor view

這是我要完成的工作:

我的MVC 5應用程序接受來自其用戶的調查。 我繼承了大致如下所示的EF模型:

調查

  • 問題
    • 答案

我被要求通過將NestedSurveyId添加到Answer對象來使調查動態化。 選擇該答案后,通過將NestedSurveyId傳遞給控制器​​來調用局部視圖,從而在下面顯示一個新的Survey模型。

該部分全部有效(顯示)。 問題是當將新添加的模型發送到控制器時,我不知道如何從頁面中提取新模型。

我不知道這樣做是否是個好主意。 不添加這樣的嵌套Survey模型的原因(也許是不好的)...

調查

  • 問題
    • 答案
      • 調查

....有時候我們有20個問題,恐怕查詢會殺死數據庫。

當客戶在我們的網站上注冊並且帖子以該簽名發送到管理員時,這些調查將得到答復

[HttpPost]
    public ActionResult AddCompany(AddCompanyModel model)

通過以下方式獲得注入的調查

public ActionResult ShowNestedSurvey(int surveyId)

這是阿賈克斯

@section scripts

{

    $(":radio").click(function() {

        var questionId = this.getAttribute("data-questionId");
        var nestedSurveyId = this.getAttribute("data-nestedSurveyId");
        if (nestedSurveyId != null && nestedSurveyId > 0) {

            $.ajax({
                url: "/Register/jenc/company/ShowNestedSurvey",
                type: "GET",
                dataType: "html",
                data: "answerId=" + nestedSurveyId,
                traditional: true,
                contentType: "application/json; charset=utf-8",
                success: function(data) {
                    $('#' + questionId).html(data);
                },
                error: function() {
                    $('#' + questionId).html("There was an error getting the survey question.");
                }
            });
        } else {
            $('#' + questionId).html("");
        }

    });

    $("form").submit(function(event) {
        alert('submitting');
        return;
    });
</script>

}

我很高興發布代碼,但這是我的第一篇文章,我不確定它是否會有所幫助或使事情變得混亂。

感謝您的幫助/建議。

$(":radio").click(function() {
        var questionId = this.getAttribute("data-questionId");
        var nestedSurveyId = this.getAttribute("data-nestedSurveyId");  
        var url="/Register/jenc/company/ShowNestedSurvey/";
        var Message="";
if (nestedSurveyId != null && nestedSurveyId > 0) {
     $.ajax({
     url: url,
     data: { answerId: nestedSurveyId},
     cache: false,
     type: "POST",
    success: function (data){
       Message=data;
         },
    error: function (){
        Message="There was an error getting the survey question.";

         }
    });

questionId.html(Message);

 $("form").submit(function(event) {
        alert('submitting');
        return;
    });
}
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM