下拉列表更改后更新视图(ViewBags)ASP MVC

时间:2014-04-15 20:04:39

标签: c# javascript asp.net asp.net-mvc asp.net-mvc-4

这是我做的第一个asp MVC项目。我以前用JAVA开发,更新值更容易。例如,我可以使用MyeditText.set或anything.set来更改值。

我的问题是:我在我的网络应用程序上使用登录页面,一个人使用用户名和密码登录后,我重定向到评估(表格)

private Db database = new Db();

    //
    // GET: /Assessment/
    [Authorize]
    public ActionResult Index()
    {
        ViewBag.Message = "Welcome to your assessment ";
        int assessment = 1;

        // DropDownList TYPE ANSWER
        string queryTA = "Select distinct TA.type_answer_id, TA.type_name from pfi_type_answer TA, pfi_assessment ASS, pfi_form FO, pfi_contain_answer CA, pfi_contain_question CQ, pfi_question QU where ASS.form_id = FO.form_id and FO.form_id = CQ.form_id and CQ.question_id = QU.question_id and QU.question_id = CA.question_id and TA.type_answer_id = CA.type_answer_id and ASS.assessment_id = "+ assessment;
        List<SelectListItem> DropListTA = new List<SelectListItem>();
        List<object[]> ListTA = database.Select(queryTA);

        foreach (object[] s in ListTA)
        {
            DropListTA.Add(new SelectListItem { Text = s[1].ToString(), Value = s[0].ToString() });
        }


        ViewBag.formListTA = DropListTA;

        // DropDownList LANGUAGE
        string queryL = "SELECT * FROM PFI_LANGUAGE";
        List<SelectListItem> DropListL = new List<SelectListItem>();
        List<object[]> ListL = database.Select(queryL);

        foreach (object[] s in ListL)
        {
            DropListL.Add(new SelectListItem { Text = s[1].ToString(), Value = s[0].ToString() });
        }

        ViewBag.formListL = DropListL;

        int TypeAnswer = (int)ListTA.ElementAt(0).GetValue(0);


        string queryC = "Select count(*) from PFI_ANSWER AN, PFI_TYPE_ANSWER TA where TA.type_answer_id = AN.type_answer_id and TA.type_answer_id= " + TypeAnswer;
        //Select count(*) from PFI_ANSWER AN, PFI_TYPE_ANSWER TA, pfi_assessment ASS, pfi_form FO, pfi_question QU, pfi_contain_question CQ, pfi_contain_answer CA where TA.type_answer_id = AN.type_answer_id and ASS.form_id = FO.form_id and FO.form_id = CQ.form_id and QU.question_id = CQ.question_id and QU.question_id = CA.question_id and AN.answer_id = CA.answer_id and TA.type_answer_id = 1 and ASS.assessment_id = 1;
        int countAnswers = database.Count(queryC);
        ViewBag.countAnswersVB = countAnswers;

        //String[] tableValue = new String[countAnswers];
        //string queryAn = "Select answer from PFI_ANSWER AN, PFI_TYPE_ANSWER TA where TA.type_answer_id = AN.type_answer_id and TA.type_answer_id=  " + TypeAnswer;
        string queryAn = "Select answer from PFI_ANSWER AN, PFI_TYPE_ANSWER TA, pfi_assessment ASS, pfi_form FO, pfi_question QU, pfi_contain_question CQ, pfi_contain_answer CA where TA.type_answer_id = AN.type_answer_id and ASS.form_id = FO.form_id and FO.form_id = CQ.form_id and QU.question_id = CQ.question_id and QU.question_id = CA.question_id and TA.type_answer_id = CA.type_answer_id and ASS.assessment_id = 1 and TA.type_answer_id = " + TypeAnswer +  " group by answer";
        List<object[]> ListAn = database.Select(queryAn);
        ViewBag.tableHeaderVB = ListAn;


        //Question and Radio buttons
        String queryQ = "Select distinct question_number, question from PFI_ANSWER AN, PFI_TYPE_ANSWER TA, pfi_assessment ASS, pfi_form FO, pfi_question QU, pfi_contain_question CQ, pfi_contain_answer CA where TA.type_answer_id = AN.type_answer_id and ASS.form_id = FO.form_id and FO.form_id = CQ.form_id and QU.question_id = CQ.question_id and QU.question_id = CA.question_id and TA.type_answer_id = CA.type_answer_id and ASS.assessment_id = 1 and TA.type_answer_id = " + TypeAnswer;
        List<object[]> ListQuestion = database.Select(queryQ);
        ViewBag.questionVB = ListQuestion;

        return View();
    }

这是索引页面的控制器代码,

cshtml页面的代码:

<script type="text/javascript">
$(function () {
    $('#formListL').change(function () {
        var selectedLanguage = $(this).val();
        alert("Alert Box Language " + selectedLanguage);
    });

    $('#formListTA').change(function () {
        var selectedVal = $(this).val();
        var selectedLanguage = $('#formListL').val();
        //alert("Alert Box Type Answer " + selectedVal);
        $.ajax({
            type: 'POST',
            url: "/Assessment/Index",
            data: { 'valueTA': selectedVal, 'valueL': selectedLanguage },
            success: function (data) {
                alert("Alert Box Type Answer " + selectedVal);
            }
        });
     });
});

@{
    ViewBag.Title = "Welcome to your assessment";
}

<h2>Index</h2>
<h2>@ViewBag.Message</h2>

<label>Answers</label>
@Html.DropDownList("formListTA")
<label class= "marge_left">Language</label>
@Html.DropDownList("formListL")



@using (Html.BeginForm("LogIn", "Home", FormMethod.Post))
{
<div class ="containTable">
<table border="1" frame="void" rules="rows" class= "assess">
    <tr>
    <th></th>
    <th></th>
    @foreach (object[] s in ViewBag.tableHeaderVB)
    {
        <th>
            @s[0]
        </th>
    }
    </tr>


    @foreach (object[] q in ViewBag.questionVB)
    {
    <tr>

        <td>
            @q[0].
        </td>

        <td>
            <a href="#***" class= "linkTable" id= @q[0]>@q[1]</a>
        </td>

            @for (int i = 0; i < ViewBag.countAnswersVB; i++)
            {
        <td>
            <input type="radio" name=@q[0] id=@q[0]@i class="css-checkbox" />
            <label for=@q[0]@i class="css-label"></label>
        </td>
            }

    </tr>
    }

</table>
</div>

<div class = "Submit">
<input type="submit" class="submit" value="Submit" name="Submit" />
</div>

}

一切正常,我得到了我想要的东西

http://image.noelshack.com/fichiers/2014/16/1397592205-pritn1.png

现在,如果我更改下拉列表的值,我希望能够刷新表格,例如,如果我更改了答案的下拉列表的值。

http://image.noelshack.com/fichiers/2014/16/1397592206-pritn2.png

我想要刷新表格,为此我尝试做一个JavaScript函数(你可以在cs-html文件中看到它)并且我做了一个新的Index方法:

 [HttpPost]
    [Authorize]
    public ActionResult Index(String valueTA, String valueL)
    {

        int value = Convert.ToInt32(valueTA);
        int valueLan = Convert.ToInt32(valueL);
        int assessment = 1;

        // DropDownList TYPE ANSWER
        string queryTA = "Select distinct TA.type_answer_id, TA.type_name from pfi_type_answer TA, pfi_assessment ASS, pfi_form FO, pfi_contain_answer CA, pfi_contain_question CQ, pfi_question QU where ASS.form_id = FO.form_id and FO.form_id = CQ.form_id and CQ.question_id = QU.question_id and QU.question_id = CA.question_id and TA.type_answer_id = CA.type_answer_id and ASS.assessment_id = " + assessment;
        List<SelectListItem> DropListTA = new List<SelectListItem>();
        List<object[]> ListTA = database.Select(queryTA);

        foreach (object[] s in ListTA)
        {
            DropListTA.Add(new SelectListItem { Text = s[1].ToString(), Value = s[0].ToString() });
        }


        // DropDownList LANGUAGE
        string queryL = "SELECT * FROM PFI_LANGUAGE";
        List<SelectListItem> DropListL = new List<SelectListItem>();
        List<object[]> ListL = database.Select(queryL);

        foreach (object[] s in ListL)
        {
            DropListL.Add(new SelectListItem { Text = s[1].ToString(), Value = s[0].ToString() });
        }

        ViewBag.formListL = DropListL;

        ViewBag.formListTA = DropListTA;
        string queryC = "Select count(*) from PFI_ANSWER AN, PFI_TYPE_ANSWER TA where TA.type_answer_id = AN.type_answer_id and TA.type_answer_id= " + value;
        //Select count(*) from PFI_ANSWER AN, PFI_TYPE_ANSWER TA, pfi_assessment ASS, pfi_form FO, pfi_question QU, pfi_contain_question CQ, pfi_contain_answer CA where TA.type_answer_id = AN.type_answer_id and ASS.form_id = FO.form_id and FO.form_id = CQ.form_id and QU.question_id = CQ.question_id and QU.question_id = CA.question_id and AN.answer_id = CA.answer_id and TA.type_answer_id = 1 and ASS.assessment_id = 1;
        int countAnswers = database.Count(queryC);
        ViewBag.countAnswersVB = countAnswers;

        String queryAn = "Select answer from PFI_ANSWER AN, PFI_TYPE_ANSWER TA, pfi_assessment ASS, pfi_form FO, pfi_question QU, pfi_contain_question CQ, pfi_contain_answer CA where TA.type_answer_id = AN.type_answer_id and ASS.form_id = FO.form_id and FO.form_id = CQ.form_id and QU.question_id = CQ.question_id and QU.question_id = CA.question_id and TA.type_answer_id = CA.type_answer_id and ASS.assessment_id = 1 and TA.type_answer_id = " + value + " group by answer";
        List<object[]> ListAn = database.Select(queryAn);
        ViewBag.tableHeaderVB = ListAn;

        //Question and Radio buttons
        String queryQ = "Select distinct question_number, question from PFI_ANSWER AN, PFI_TYPE_ANSWER TA, pfi_assessment ASS, pfi_form FO, pfi_question QU, pfi_contain_question CQ, pfi_contain_answer CA where TA.type_answer_id = AN.type_answer_id and ASS.form_id = FO.form_id and FO.form_id = CQ.form_id and QU.question_id = CQ.question_id and QU.question_id = CA.question_id and TA.type_answer_id = CA.type_answer_id and ASS.assessment_id = 1 and TA.type_answer_id = " + value;
        List<object[]> ListQuestion = database.Select(queryQ);
        ViewBag.questionVB = ListQuestion;
        //return PartialView();

        return View();
    }

我只想刷新表格(Viewbags),我将进入函数&#34; public ActionResult Index(String valueTA,String valueL)&#34;当我更改我的下拉列表的值但没有发生任何事情..

希望你能帮助我。 谢谢。

0 个答案:

没有答案