下拉列表可以触发部分视图以在mvc中的创建视图表单上更新吗? [英] Can a Drop Down List Trigger A Partial View To Update on A Create View Form In mvc?

查看:56
本文介绍了下拉列表可以触发部分视图以在mvc中的创建视图表单上更新吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要应用于创建视图表单的选项是一个选项,用户可以在从记录的下拉列表中选择一项之前,将记录写入数据库,然后从下拉列表中选择他们的记录创建视图表单上的列表列出了隐藏在该表单上的局部视图,并显示了视图包中的值.

由于我在寻求帮助之前已经对此有所帮助,因此对于目标是什么以及在创建上可能会阻止发生的问题很清楚,因为这可能是mvc方法中的一个限制,因为mvc是适用于手机小型设备.我正在完整的PC上构建此应用程序.有人说可以做到.我正在使用viewbag,因为这是我唯一了解的内容.我不了解模型概念,也不知道ajax是什么或如何使用.我只知道如何执行javascript功能,就像我看到的示例w3schools一样.

我到目前为止所拥有的,

对于我的局部视图,我有与此类似的东西:称为_IssuesListPartial

<table cellpadding="1" border="1">
    <tr>
        <th>
           Issues List 
        </th>

    </tr>

    @foreach (SystemX.Models.VUE_ISSUE_LIST_FULL item in ViewBag.IssuesList)
    {
        <tr>
            <td>
                @item.ISSUE_DISCRIPTION
            </td>            
        </tr>
    }
</table>

在后面的我创建的Controller代码中,在create选项下有一些项目 我有一个功能可以列出视图包.

    private List<VUE_ISSUE_LIST_FULL> Get_IssuesList(int? VID)
    {

        return db.VUE_ISSUE_LIST_FULL.Where(i => i.ID == VID).ToList();
    }

然后我将结果分配给另一个以动作结果"开头的函数中的视图包.它叫做UpdateIssuesList:

这是在控制器中的create逻辑下.我看到类似的东西有一个[HttpPost],但是当create表单尝试呈现时出现错误,因此我将其更改为[HttpGet],并且可以正常工作了负载.我不确定[HttpGet]或[HttpPost]是否正确..我只知道创建表单会用[HttpGet]加载

所以在控制器中的创建功能下,我有这个.请记住,我还有脚手架构建器为创建视图制作的所有其他内容.我只是在下面添加了此代码.

    [HttpGet]
    public ActionResult UpdateIssuesList(int? VID)
    {


        ViewBag.IssuesList = Get_IssuesList(VID);
        return PartialView("_IssuesListPartial",ViewBag.IssuesList);


    } 

在创建表单"视图中,我只有很少的项目...表单上显示该区域时希望显示的区域:

在public ActionResult Create函数下,我有:

 public ActionResult Create(int RES_ID, FormCollection Collection, [Bind(Include = "R.... and so on...

 UpdateIssuesList(int.Parse(Collection["RES_ID"]));

,在公共ActionResult Create()下,我有: 函数对UpdateIssuesList函数的调用..我只是发送一个随机值,该值将建立一个空白列表.

UpdateIssuesList(808);

这是创建视图表单的代码,我要在其中渲染部分视图:

        <div class="col-sm-6">

            <div class="form-horizontal" style="display:none" id="PV_IssueList">

                @{ Html.RenderAction("UpdateIssuesList",new {@VID = 1 });}

            </div>
        </div>

在创建视图表单的脚本部分中,我还具有当用户对创建视图表单上的下拉列表进行更改时运行的代码.最终目标还是当用户单击并更改下拉列表中的项目时…值获取的ID值将发送到列表制作者并生成新的最后一个,并将新列表传递到部分视图和表单已更新.

    $(document).ready(function () {
        $('#RES_ID').change(function ()
        {
            debugger;
            $("#PV_IssueList").show(); // Shows List
            $.post("/Create/_IssuesListPartial?VID=1")


        });
    })

mvc是否可以从中更新创建内容,因为那里什么也没有?没有代码或事件触发器可以在提交表单之前刷新部分视图,因此使用mvc可能无法实现此目的,并且我在浪费时间.我刚刚将人们捣碎在一起,因为我没有看到在create视图中呈现列表的实际示例,所以看到了一些想法,因此我在这里追逐风车.感谢您抽出宝贵的时间阅读这篇文章.

因此,当我运行此代码时,我在选择一个值后进入页面,该视图是部分视图的顶部文本,但位于列表数据上.当我单步执行操作时,我会在变量和填充物中获取数据,但在创建视图表单中却看不到屏幕

解决方案

由于某种原因,答案被删除了,所以我将尝试以不同的方式回答.我使它工作的方式首先是在用户的帮助下进行的,此表格和其他表格以及一些研究.如建议的那样,使用ajax语言是一个很大的帮助.

这是后面的Ajax代码:

$(document).ready(function () {
    $('#RES_VID').change(function ()
    {

        debugger;

        $.ajax(

            {
                url: '@Url.Action("UpdatePartialViewList")',
                type: 'GET',
                data: { VID: $('#RES_VID').val() },

                success: function (partialView)
                {
                    $('#PV_WidgetList').html(partialView);
                    $('#PV_WidgetList').show();
                }
            });

因此,要弄清这里发生的事情,是我在控制器中有一个名为UpdatePartialViewList的函数,该函数将部分视图及其视图包一起传回:

控制器中的功能是这样:

[HttpGet]
public ActionResult UpdatePartialViewList(int? VID)
{           

    ViewBag.AList = Get_List(VID);
    return PartialView("_WidgetListPartial",ViewBag.AList);


}

因此,使用这两项,您看起来就像是一个占位符.因为这会将信息传递回页面.所以在我的创建视图中,我有这个:

UpdatePartialViewList(int.Parse(Collection ["RES_VID"]))); 在您要显示部分视图的创建视图"屏幕上

    <div class="col-sm-6">

        <div class="form-horizontal" style="display:none" id="PV_WidgetList">

            @{ Html.RenderAction("UpdatePartialViewList");}



        </div>
    </div>

希望此帮助可用于Webforms,并试图获取整个MVC.祝你好运!

The option I am trying to apply to a create view form is one on which the user selects an item from the drop down list on a create from before a record is written to the database and as they select their records from the dropdown list on the create view form a partial view that is hidden on this form becomes visible and shows values that are in a viewbag.

As I have seeked help for this before I don't this the question was clear on what the goal was and that this is on the create which may prevent from happening as this may be a limitation within the mvc methods since mvc is for phones small devices. I am building this application on a full pc. Some say it can be done. I am using viewbag as that is the only thing I understand.. I don't understand the model concept and I don't know what ajax is or how to use. I just know how to do the javascripting functions as I have seen examples w3schools.

What I have so far,

I have something similar to this for my partial view: Called _IssuesListPartial

<table cellpadding="1" border="1">
    <tr>
        <th>
           Issues List 
        </th>

    </tr>

    @foreach (SystemX.Models.VUE_ISSUE_LIST_FULL item in ViewBag.IssuesList)
    {
        <tr>
            <td>
                @item.ISSUE_DISCRIPTION
            </td>            
        </tr>
    }
</table>

In my crated Controller code behind I have under the create option a few items I have a function that makes a list for the view bag.

    private List<VUE_ISSUE_LIST_FULL> Get_IssuesList(int? VID)
    {

        return db.VUE_ISSUE_LIST_FULL.Where(i => i.ID == VID).ToList();
    }

I then assign the results to the view bag in another function that starts with the words action results. Its called UpdateIssuesList:

This is under the create logic in the controller.I saw something similar they had a [HttpPost] but I get an error when the create form is trying to render so I changed it to [HttpGet] it works and the create for loads. I am not sure that the [HttpGet] or [HttpPost] does..I just know that the create forms loads with [HttpGet]

So in the controller under the create functions I have this. Please keep in mind I have all of the other stuff that the scaffold builder made for the create view. I just added this code behind under it.

    [HttpGet]
    public ActionResult UpdateIssuesList(int? VID)
    {


        ViewBag.IssuesList = Get_IssuesList(VID);
        return PartialView("_IssuesListPartial",ViewBag.IssuesList);


    } 

In the create form view I have as few items... The area on the form where I would like this list to show when it is made visible:

under function public ActionResult Create I have:

 public ActionResult Create(int RES_ID, FormCollection Collection, [Bind(Include = "R.... and so on...

 UpdateIssuesList(int.Parse(Collection["RES_ID"]));

and under the public ActionResult Create()I have: the function call to the UpdateIssuesList funtion.. I just send a random value that will build a blank list.

UpdateIssuesList(808);

Also

This is the code behind for the create view form where I want to render the partial view:

        <div class="col-sm-6">

            <div class="form-horizontal" style="display:none" id="PV_IssueList">

                @{ Html.RenderAction("UpdateIssuesList",new {@VID = 1 });}

            </div>
        </div>

And I also have in the script section of the create view form the code behind that run when the user makes a change to the drop down list on the create view form. The end goal again is when the user click and changes the item on the dropdown list... the value gets id values gets sent to the list maker and and generates a new last and that new list is passed to the partial view and the form is updated.

    $(document).ready(function () {
        $('#RES_ID').change(function ()
        {
            debugger;
            $("#PV_IssueList").show(); // Shows List
            $.post("/Create/_IssuesListPartial?VID=1")


        });
    })

is it possible for mvc to update a create from as there is nothing there ? No code or event trigger that will refreshes the partial view before the form is submitted So This may not be possible with mvc and I am wasting my time trying. I have just smashed together peoples Ideas that I have seen as I have not seen an actual example of a list being rendered in the create view so I way be chasing windmills here. Thanks for taking time to read this.

So when I run this code I get on the page after I selected a value the head text of the partial view but on list data. When I step through I get data in the variables and stuff but not on screen in the create view form

解决方案

Ok for some reason the answer was deleted so I will try to answer it differently. The way I got this to work was first with the help of user on this form and other forms as well along with some research. Using the ajax language was a big help as suggested.

Here is the Ajax code behind:

$(document).ready(function () {
    $('#RES_VID').change(function ()
    {

        debugger;

        $.ajax(

            {
                url: '@Url.Action("UpdatePartialViewList")',
                type: 'GET',
                data: { VID: $('#RES_VID').val() },

                success: function (partialView)
                {
                    $('#PV_WidgetList').html(partialView);
                    $('#PV_WidgetList').show();
                }
            });

So to break down whats going on here is that I have in my controller a function called UpdatePartialViewList this function passes back a partial view along with its view bag:

The Function in the controller is this:

[HttpGet]
public ActionResult UpdatePartialViewList(int? VID)
{           

    ViewBag.AList = Get_List(VID);
    return PartialView("_WidgetListPartial",ViewBag.AList);


}

So with these two items you have like a place holder it seems. because this will pass the information back to the page. So in my create view I have this:

UpdatePartialViewList(int.Parse(Collection["RES_VID"])); On Your Create View Screen where you want your partial view to display

    <div class="col-sm-6">

        <div class="form-horizontal" style="display:none" id="PV_WidgetList">

            @{ Html.RenderAction("UpdatePartialViewList");}



        </div>
    </div>

Hopefully this help other that are use to webforms and are trying to get the whole mvc thing. Best wishes!

这篇关于下拉列表可以触发部分视图以在mvc中的创建视图表单上更新吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆