简体   繁体   English

MVC和Ajax在Razor视图中注入模型

[英]MVC and Ajax injected Models in Razor view

Here is what I'm trying to accomplish: 这是我要完成的工作:

My MVC 5 application accepts surveys from it's users. 我的MVC 5应用程序接受来自其用户的调查。 I inherited the EF models which are laid out roughly like this: 我继承了大致如下所示的EF模型:

Survey 调查

  • Questions 问题
    • Answers 答案

I have been asked to make the survey dynamic by adding a NestedSurveyId to the Answer object. 我被要求通过将NestedSurveyId添加到Answer对象来使调查动态化。 when that answer is selected a new Survey model is displayed underneath by calling a partial view by passing the NestedSurveyId to the controller. 选择该答案后,通过将NestedSurveyId传递给控制器​​来调用局部视图,从而在下面显示一个新的Survey模型。

That part all works (the display). 该部分全部有效(显示)。 The problem is that I can't figure out how to extract the newly added model from the page when it is sent to the controller. 问题是当将新添加的模型发送到控制器时,我不知道如何从页面中提取新模型。

I don't know whether doing it this way is a good idea or not. 我不知道这样做是否是个好主意。 The reason (perhaps a bad one) for not adding nested Survey models like this... 不添加这样的嵌套Survey模型的原因(也许是不好的)...

Survey 调查

  • Questions 问题
    • Answers 答案
      • Survey 调查

....is that we sometimes have 20 questions and I'm afraid the query will kill the database. ....有时候我们有20个问题,恐怕查询会杀死数据库。

These surveys are answered when a customer signs up on our site and the post goes to a controller with this signature 当客户在我们的网站上注册并且帖子以该签名发送到管理员时,这些调查将得到答复

[HttpPost]
    public ActionResult AddCompany(AddCompanyModel model)

The injected survey is obtained by calling this 通过以下方式获得注入的调查

public ActionResult ShowNestedSurvey(int surveyId)

This is the ajax 这是阿贾克斯

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

} }

I'm happy to post the code, but this is my first post and I'm not sure it would be helpful or just clutter things up. 我很高兴发布代码,但这是我的第一篇文章,我不确定它是否会有所帮助或使事情变得混乱。

Thanks for any help/suggestions you have. 感谢您的帮助/建议。

$(":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