简体   繁体   English

发布部分用JQuery加载的ASP.NET MVC页面

[英]Post an ASP.NET MVC page partially loaded with JQuery

So I'm working on an Advert listing page where I need to allow the user to add rooms to an advert dynamically without posting back the page. 因此,我正在“广告列表”页面上工作,在该页面上,我需要允许用户动态添加房间到广告中,而无需回发页面。 I want to do this with Ajax to reduce the amount of navigation required thus improving the UX. 我想用Ajax做到这一点,以减少所需的导航量,从而改善UX。

Initially I looked at using the Ajax helper methods to load the dynamic elements of the page. 最初,我着眼于使用Ajax帮助器方法加载页面的动态元素。 However, this didn't work so well since MVC/HTML doesn't support nested forms. 但是,由于MVC / HTML不支持嵌套表单,因此效果不佳。

Ajax.BeginForm inside Html.BeginForm Html.BeginForm中的Ajax.BeginForm

I ended up using some JQuery to load the rooms and Add/Remove rooms. 我最终使用了一些JQuery来加载房间和添加/删除房间。 The JQuery calls an action on the controller to load the rooms after adding/removing the rooms. 在添加/删除房间之后,JQuery调用控制器上的操作以加载房间。

$("[name='RemoveRoom']").click(function () {
    var url = $(this).data('url');

    $('#roomsWrapper').load(url);
});


[HttpGet]
public ActionResult GetRooms(int Id)
{
    var viewModel = this.roomAvailableAdvertStore.FindById(Id);

    return PartialView("_Rooms", viewModel.Rooms);
}

The problem I'm now facing is that these dynamically loaded elements are not being bound to the View Model when I submit the form. 我现在面临的问题是,当我提交表单时,这些动态加载的元素没有绑定到视图模型。 To explain more clearly 为了更清楚地解释

<form>
    @Html.HiddenFor(m => m.AdvertId); 
    <div id=”roomsWrapper”>
       // These data items are not being bound to the model.
       @Html.Partial(“_Rooms”, Model.Rooms);
    </div>
    <input type="submit" />
<form>

_Rooms.cshtml partial page looks like this: _Rooms.cshtml部分页面如下所示:

@model List<RoomViewModel>

@for (int roomIndex = 0; roomIndex < Model.Count(); roomIndex++)
{
    @Html.HiddenFor(m => m[roomIndex].RoomId)
}

The form posts back to an action with the following signiture: 表单回发给具有以下签名的操作:

public async Task<ActionResult> Update(RoomAvailableAdvertViewModel model)
{
   ...
}

The view model looks like this: 视图模型如下所示:

public class RoomAvailableAdvertViewModel
{
    public int AdvertId { get; set; }
    public List<RoomViewModel> Rooms { get; set; }
}

I managed to solve this issue thanks to the links left in the comments. 感谢评论中留下的链接,我设法解决了这个问题。

Basically this issue occurs because the default ASP.net MVC model binder is only able to bind to a collection of complex types if they can be uniquely identified. 基本上会发生此问题,因为默认的ASP.net MVC模型绑定程序仅能够绑定到复杂类型的集合(如果可以唯一地标识它们)。 This is all explain far more articulately by Phil Haack in the following article: 在以下文章中,Phil Haack对此进行了更清晰的阐述:

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/ http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

However, the limitation with Phil's approach is that he applies a numeric index to each element in the collection. 但是,Phil方法的局限性在于他将数字索引应用于集合中的每个元素。 This approach fails when you attempt to add items dynamically, as the index won't be unique. 当您尝试动态添加项目时,此方法会失败,因为索引不会唯一。 The second link given in the comments solves this problem by applying a Guid to each element in the collection, ensuring that each element in the collection has a unique key. 注释中给出的第二个链接通过将Guid应用于集合中的每个元素来解决此问题,并确保集合中的每个元素都有唯一的键。

http://blogs.interknowlogy.com/2014/08/01/mvc-series-part-1-dynamically-adding- items-part-1/ http://blogs.interknowlogy.com/2014/08/01/mvc-series-part-1-dynamically-adding- items-part-1 /

It would be interesting if the Razor view engine could be extended to mitigate this issue occurring. 如果可以扩展Razor视图引擎来减轻此问题的发生,将会很有趣。

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

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