简体   繁体   English

从PartialView MVC4获取数据

[英]get data from PartialView MVC4

I'm working with (ASP.Net MVC4), and I'm stuck in how to get info from partial view. 我正在使用(ASP.Net MVC4),但是我陷入了如何从局部视图中获取信息的问题。 I mean I have a my viewmodel 我的意思是我有一个ViewModel

public class ShowHomeViewModel
{
    public int ID_ClientTypeID { get; set; }
    public string ReservationDate { get; set; }
    public short ClientNum { get; set; }
            public string ClientPhone { get; set; }

    public List<HomeViewModel> ReservedHuts { get; set; }
}

I have a partial view where I'm showing all de data from HomeViewModel, it show's great, the problem is when I try to recover the list of ReservedHuts from the view: 我有一个局部视图,其中显示了HomeViewModel的所有数据,显示效果很好,问题是当我尝试从视图中恢复ReservedHuts列表时:

@model HutReservation.ViewModel.ShowHomeViewModel
    <table>
        <tr>
            @{Html.RenderPartial("_Reservations", Model.ReservedHuts);}
        </tr>
    </table>

In my view I show the list and change some data, but when I click on the button and go to the New Method bellow, turns out that the ReservedHuts (the list of HomeViewModel) is null 在我的视图中,我显示了列表并更改了一些数据,但是当我单击按钮并转到“ New Method”下面时,结果表明ReservedHuts(HomeViewModel的列表)为null

[HttpPost] //cambiar la pagina para el partial view
    public ActionResult New(ShowHomeViewModel vm) // <- this vm is null
    {

        foreach (HomeViewModel hvm in vm.ReservedHuts)
        {

        }
        return View("ConfirmNew", vm);
    }

I'm really stuck here, Thanks for your help in advance 我真的被困在这里,谢谢您的提前帮助

The problem here is that your form data cannot be bound to ShowHomeViewModel . 这里的问题是您的表单数据无法绑定到ShowHomeViewModel This is because the context of Model has changed when you render your partial (the model context has changed from ShowHomeViewModel to HomeViewModel). 这是因为渲染局部时Model的上下文已更改(模型上下文已从ShowHomeViewModel更改为HomeViewModel)。 MVC uses conventions to bind the properties by default (you can make your own model binders if you wish, which use whatever methods you want to bind). MVC默认情况下使用约定来绑定属性(如果需要,可以创建自己的模型绑定程序,该绑定程序可以使用要绑定的任何方法)。 By Default, when you make an EditorFor a property of your model, MVC will name the form field appropriate so that it can be bound back to the model context it was created from. 默认情况下,当您创建EditorFor模型的属性时,MVC会适当地命名表单字段,以便可以将其绑定回其创建时所依据的模型上下文。

So if you skip the partial, you can just run your foreach in the parent view, and you could probably still bind to ShowHomeViewModel because it will name the form fields like this: 因此,如果跳过部分视图,则可以仅在父视图中运行foreach,并且您可能仍可以绑定到ShowHomeViewModel因为它将像这样命名表单字段:

<input name="ReservedHuts_HutId">

However, if you use the partial and change your model context, it will render the same input like this: 但是,如果您使用partial并更改模型上下文,它将呈现相同的输入,如下所示:

<input name="HutId">

So, MVC cannot (by default) bind the value of HutId to ShowHomeViewModel because there is no property called HutId (But there is HutId property on HomeViewModel, so it will bind successfully!) 因此,MVC不能(默认)绑定的值HutIdShowHomeViewModel因为没有所谓的财产HutId (但对HomeViewModel HutId属性,所以它会成功绑定!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM