麻烦路过视图和控制器之间复杂的数据在ASP.NET MVC [英] Trouble passing complex data between view and controller in ASP.NET MVC
问题描述
下面是我在ASP.NET MVC模型真正的简化,我认为这将有助于集中在问题:
Here's a simplification of my real models in ASP.NET MVC, that I think will help focus in on the problem:
让我们说我有两个域对象:
Let's say I have these two domain objects:
public class ObjectA
{
public ObjectB ObjectB;
}
public class ObjectB
{
}
我也有一种观点认为,可以让我创建一个新的对象A和包括从可能ObjectBs列表中选择一个对象B
I also have a view that will allow me to create a new ObjectA and that includes selecting one ObjectB from a list of possible ObjectBs.
我创建了一个新类的可能性,这个名单来装饰对象A,这真的是我的视图模型,我猜。
I have created a new class to decorate ObjectA with this list of possibilities, this is really my view model I guess.
public class ObjectAViewModel
{
public ObjectA ObjectA { get; private set; }
public SelectList PossibleSelectionsForObjectB { get; private set; }
public ObjectAViewModel(ObjectA objectA, IEnumerable<Location> possibleObjectBs)
{
ObjectA = objectA;
PossibleSelectionsForObjectB = new SelectList(possibleObjectBs, ObjectA.ObjectB);
}
}
现在,什么是构建我的视图和控制器允许用户在视图中选择一个对象B,然后让控制器保存对象A与对象B的选择(对象B已经存在,并保存)的最佳方法是什么?
Now, what is the best way to construct my view and controller to allow a user to select an ObjectB in the view, and then have the controller save ObjectA with that ObjectB selection (ObjectB already exists and is saved)?
我试图创建一个类型,ObjectAViewModel的强类型的视图,并绑定Html.DropDownList到Model.PossibleSelectionsForObjectB。这是好的,而且我可以选择的对象就好了。但要回控制器是我在哪里挣扎。
I tried creating a strongly-typed view of type, ObjectAViewModel, and binding a Html.DropDownList to the Model.PossibleSelectionsForObjectB. This is fine, and the I can select the object just fine. But getting it back to the controller is where I am struggling.
尝试性解决方案1:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(ObjectAViewModel objectAViewModel)
这里此问题在于objectAViewModel.ObjectA.ObjectB属性为空。我想这势必将这个属性DropDownList中,将更新,当用户在视图中选择此模式,但它不是出于某种原因。
This problem here is that the objectAViewModel.ObjectA.ObjectB property is null. I was thinking the DropDownList which is bound to this property, would update the model when the user selected this in the view, but it's not for some reason.
尝试性解决方案2:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(ObjectA objectA)
这里此问题在于ObjectA.ObjectB属性为空。再次,我想也许DropDownList的选择会更新此。
This problem here is that the ObjectA.ObjectB property is null. Again, I thought maybe the DropDownList selection would update this.
我也使用在上述各方案的的UpdateModel方法,没有运气试过。没有任何人有什么想法?我猜我缺少一个绑定或一些地方...
I have also tried using the UpdateModel method in each of the above solutions, with no luck. Does anyone have any ideas? I'm guessing I'm missing a binding or something somewhere...
谢谢!
推荐答案
一些调查研究后,它看起来并不像这是一个情况下,ASP.NET MVC会照顾我。或许有一个数据服务绑定模式,我可以使用(所以MVC会自动抓取合适的对象了内存,基于什么在下拉列表中选择),但现在,我可以在控制器处理起来解决这个问题:
After some more research it doesn't look like this is a case ASP.NET MVC will take care of for me. Perhaps there is a data service binding model I can use (so MVC would automatically grab the appropriate object out of memory, based on what was selected in the dropdown), but for now, I can fix this by handling it in the controller:
- 从使用Controller.ModelState下拉获取所选择的项目
- 重新装入对象B从底层数据服务
- 分配的对象B到ObjectA.ObjectB
- 保存对象A
所以我的控制器方法看起来现在这个样子:
So my controller method looks like this now:
根据来自LukLed评论主编
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(ObjectA objectA, string objectBStr)
{
ObjectB objectB = _objBService.Get(objectBStr);
objectA.ObjectB = objectB;
_objAService.Save(objectA);
return RedirectToAction("Details", new { id = objectA.Id });
}
这篇关于麻烦路过视图和控制器之间复杂的数据在ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!