如何从子视图到主视图获取MVC中的POSTed数据?

时间:2016-10-02 18:03:44

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

我正在尝试使用VS 2013 Professional尝试将数据从子视图传递到MVC中的主视图。我使用C#和Razor作为主要语言。我试图动态地将信息从我的子视图发布到主视图,我不知道该怎么做。现在我正在使用JavaScript来发布数据:

    #partialViewList#
    @using WebApplication2.Models
    @model IEnumerable<WebApplication2.Models.OBJECT>
    <script>
        function postItO(OfficeLocation, Name, Email, Phone, NumComputers, NumMonitors) {

        form = document.createElement('form');
        form.setAttribute('method', 'POST');
        form.setAttribute('action', 'CompAndMon');
        myvar = document.createElement('input');
        myvar.setAttribute('OfficeName', "Primary Contact Name " + Name);
        myvar.setAttribute('type', 'Office');
        myvar.setAttribute('Email', "Primary Contact Email: " + Email);
        myvar.setAttribute('Phone', "Primary Contact Phone: " + Phone);
        myvar.setAttribute('NumComps', "Number of Computers: " + NumComputers);
        myvar.setAttribute('NumMons', "Number Of Monitors: " + NumMonitors);
        form.appendChild(myvar);
        document.body.appendChild(form);
        form.submit();
    }
    //I use the same general Idea when posting the other two models but so the code is not too long I will exclude them
    </script>

   <div class="panel-body col-lg-11 col-lg-offset-1" style="overflow-y: scroll;">




        @foreach (var item in Model)
        {

            if (@item.Type == 1)
            {

                var office = item as Office;
                string loc = @office.OfficeName;
                string Name = @office.OfficeNumber;
                string email = @office.ContactName;
                string phone = @office.ContactNumber;
               // int mons = @office.NumMonitors;
                //int comps = @office.NumComputers;

                <p><a onclick="postItO('@loc','@Name','@email','@phone')">@office.OfficeName</a></p>
            }
            else if (@item.Type == 2)
            {
                var computer = item as Computer;
                string lst = @computer.LastUser;
                string Nme = @computer.Name;
                string TtHD = @computer.TotalHDSpace;
                int NmUp = @computer.NumUpdates;
                int NmMn = @computer.NumMonitors;
                string FrHD = @computer.FreeHDSpace;

                <p>&ensp;&ensp;<a onclick="postItC('@lst','@Nme', '@TtHD','@FrHD','@NmMn','@NmUp')">@computer.Name</a></p>
            }
            else
            {
                var monitor = item as Monitor;
                string man = @monitor.Manufacturer;
                string mid = @monitor.ModelID;
                string SN = @monitor.SerialNum;
                int hr = @monitor.HoursON;
                string TTi = @monitor.LastTestTime;
                string TTy = @monitor.LastTestType;

                <p>&ensp;&ensp;&ensp;&ensp;<a onclick="postItM('@man', '@SN','@hr','@mid','@TTi','@TTy')">@monitor.Manufacturer</a></p>
            }
        }

</div>

我发布到名为CompAndMon的主视图。 这两个控制器看起来像这样,但我不确定是否必须添加参数,因为我以“陌生”的方式发布信息

#Home Controller#

        public ActionResult CompAndMon()
     //I think I should put parameters in here but i am not sure
    {



        return View();
    }
    public ActionResult _OCMList()
    {
        var ObjectList = new List<OBJECT>{
                         new Office() {Type =  1,ID = 1, Name1 = "Fort Collins", OfficeName = "Fort Collins", OfficeNumber = "1", ContactNumber = "555-123-5555", ContactName = "ted" } ,
                        new Computer() {Type =  2,ID = 2,Name1 = "Speed-Machine", Name = "Speed-Machine", LastUser = "Ted", NumMonitors = 1, FreeHDSpace = "12GB", NumUpdates = 0, TotalHDSpace = "50GB" } ,
                        new Monitor() {Type =  3, ID = 3,Name1 = "Sony", Manufacturer = "Sony", HoursON = 20, LastTestTime = "11pm, August 31", LastTestType = "SMPTE", ModelID = "654123", SerialNum = "a36-f45-gh325"} ,
                        new Office(){Type =  1,ID = 2, Name1 = "Denver", OfficeName = "Denver", OfficeNumber = "2", ContactNumber = "555-123-5555", ContactName = "Nick" }  ,
                        new Computer() {Type =  2, ID = 5,Name1 = "Nicks PC", Name = "Nicks PC", LastUser = "Ted", NumMonitors = 1, FreeHDSpace = "12GB", NumUpdates = 0, TotalHDSpace = "50GB"  } ,
                        new Monitor() {Type =  3, ID = 6,Name1 = "LG", Manufacturer = "LG", HoursON = 20, LastTestTime = "11pm, August 31", LastTestType = "SMPTE", ModelID = "654123", SerialNum = "a38-l87kp-g6j9"} ,
                        new Computer() {Type =  2, ID = 7,Name1 = "Ted", Name = "FastOne", LastUser = "Ted", NumMonitors = 2, FreeHDSpace = "23GB", NumUpdates = 2, TotalHDSpace = "50GB"  } ,
                        new Monitor() {Type =  3, ID = 8,Name1 = "HTC", Manufacturer = "HTC", HoursON = 20, LastTestTime = "11pm, August 31", LastTestType = "SMPTE", ModelID = "654123", SerialNum = "d77-ko9-poo77" },
                         new Monitor() {Type =  3, ID = 9,Name1 = "Panisonic", Manufacturer = "Panisonic",HoursON = 20, LastTestTime = "11pm, August 31", LastTestType = "SMPTE", ModelID = "654123", SerialNum = "h67-j567-lo99" }
         };

        return PartialView(ObjectList);
    }

在我的主视图中重要的是

#CompAndMon
///some javaScript or AJAX to grab posted values
<div class="container" id="MyPartial">
                @Html.Action("_OCMList","Home")

</div>

我是MVC的新手,所以如果有很多问题我会道歉。 所以要结束这个问题......我如何在主视图中获取已发布的变量?

1 个答案:

答案 0 :(得分:0)

您可以通过将值发布到采用PostModel参数并命名与GET相同的新操作方法来实现此目的。您可以使用属性来区分这两者。

    [HttpGet]
    public ActionResult CompAndMon()
    {
        var viewModel = new ViewModel();
        return View(viewModel);
    }

    [HttpPost]
    public ActionResult CompAndMon(PostModel model)
    {
        var viewModel = new ViewModel
        {
            Thing = model.Thing
        };
        return View(viewModel)
    }

    public class PostModel
    {
        public object Thing { get; set; }
    }

    public class ViewModel
    {
        public object Thing {get; set; }
    }

然后在你的&#34; Main&#34;查看您可以将此值检查为null,如果有值回发,则填充显示。

@model Project.Models.ViewModel

<div class="container">
    @if (Model.Thing != null)
    {
        if (thing is Office office)
        {
                <!--Display Office-->
        }
        else if (thing is Computer computer)
        {
                <!--Display Office-->
        }
        else if (thing is Monitor monitor)
        {
                <!--Display Office-->
        }
     }

    @Html.Action("_OCMList", "Home");
</div>