不必重新填充视图模型时的ModelState是无效的,由于不发送的所有数据,比如下拉框列表 [英] Having to repopulate viewmodel when modelstate is invalid due to not sending all data in such as drop down box list

查看:250
本文介绍了不必重新填充视图模型时的ModelState是无效的,由于不发送的所有数据,比如下拉框列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目我创建了一个调查,我这个调查链接到公司,并选择用户参与。我选择的调查模板,添加额外的问题,并着手调查,如开始和结束日期的信息。

In my project I create a survey, I link this survey to a company and select users to participate. I select survey template, add extra questions and sets information about the survey such as start and end date.

总而言之,结果是一个复杂的视图与从域的许多部分数据。 我创建了一个视图模型对这一观点,将包括诸如所有公司名单(因为我需要在下拉列表中选择公司)。

All in all the result is a complex view with data from many parts of the domain. I created a ViewModel for this view that would include things such as a list for all companies (because i need to select company in a drop down list).

现在的烦恼是,当我提交保存,如果我有一个的ModelState错误,那么我想重新显示,但因为所有的数据我拿来,如所有公司的名单并没有从客户端发送(即使寿该视图模型支持的话),我必须再次填充所有这些列表/变量之前再次显示的形式。

Now the annoyance is that when i submit to save, if i have a modelstate error then i want to redisplay the view but since all the data i fetched such as list of all companies was not send in from the client (even tho the viewmodel supports it) I have to again populate all those lists/variables before again displaying the form.

我想我可以创建创建一个下拉的视图,但由于下拉需要插入它的价值在我的调查下CompanyId那不是很明显无论是。

I suppose I could have created a view that created a drop down, but since the drop down needs to insert it's value into my survey under CompanyId thats not obvious either.

我觉得一个自定义的模型绑定器也可以做的伎俩。我问在这里得到了最好的解决方案的感觉在那里。如果我宁愿重新考虑整个战略。

I think a custom modelbinder can also do the trick. I am asking here to get a feeling for the best solution out there. If I should rather rethink the whole strategy.

推荐答案

一种可能性当然是一个由Tim具有自动补液建议。

One possibility is certainly the one suggested by Tim with automated rehydration.

不过,我最终拍得该方法是由蒂姆·托马斯摹这里建议:的http://timgthomas.com/2013/09/simplify-client-side-validation-by-adding-a-server/

However the approach I went for in the end is the on suggested by Tim G Thomas here: http://timgthomas.com/2013/09/simplify-client-side-validation-by-adding-a-server/

这意味着,数据永远不会离开客户端,除非它是有效的,这意味着需要对补液被除去。 它要求的Jav​​aScript这是可以接受的我,因为你永远需要再次补充水分的景色。

It means that the data will never leave the client unless it is valid, meaning need for rehydration is removed. It does require javascript which is acceptable for me since you never need to rehydrate a view again.

这篇关于不必重新填充视图模型时的ModelState是无效的,由于不发送的所有数据,比如下拉框列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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