我的动作控制器代码看起来很业余

发布于 2024-07-22 03:06:24 字数 991 浏览 6 评论 0原文

第一次发帖,

我一直在尝试 MVC abit...我有一个具有多个输入字段的视图,其中一些字段在发帖时可能为空白。

帖子控制器内的操作方法看起来像这样,

public ActionResult Filter(int? id, string firstName, string lastName, bool? isMember)

我一直在使用 DynamicQuery 扩展,该扩展一直在使用,以便在我的数据库上执行动态 Linq 查询,并且我已将其封装在传递给的 Search 对象中用于执行的数据访问层。

但是,我还有一个自定义的 ViewData 对象,该对象被传递回视图以显示输入值和查询结果。

这一切在代码中看起来有点令人讨厌,因为我必须设置搜索对象属性和 ViewDatas。

public ActionResult Filter(int? id, string firstName, string lastName, bool? isMember)  { 
var search = new Search { 
Id = id, 
FirstName = firstName, 
LastName = lastName, 
Member =  isMember 
}; 

var memberViewData = new MemberViewData { 
Id = id, 
FirstName = firstName, 
LastName = lastName, 
Member =  isMember
}; 

memberViewData.Results = _dataRepository.GetMember(search); 

return View("Search", memberViewData); 

}

我是否想得太多了,真的应该将值传递到数据访问层并填充控制器中的 ViewData,还是有我可以使用的更优雅的模式或实践?

抱歉,如果这看起来很垃圾,没有分配人来交流想法和时间来深入研究框架。

First post time,

I've been playing around with MVC abit... I have a view which has multiple input fields, some of these fields can be blank on post.

The action method inside the controller for the post looks something like this

public ActionResult Filter(int? id, string firstName, string lastName, bool? isMember)

I've been using the DynamicQuery extension which has been kicking around in order to perform dynamic Linq querys on my database and I've encapsulated this in a Search object which is passed to the data access layer for execusion.

However, I also have a customized ViewData object which is passed back to the view for displaying the input values and the results of the query.

It all looks a little nasty in code as I'm having to set both the Search object properties AND the ViewDatas.

public ActionResult Filter(int? id, string firstName, string lastName, bool? isMember)  { 
var search = new Search { 
Id = id, 
FirstName = firstName, 
LastName = lastName, 
Member =  isMember 
}; 

var memberViewData = new MemberViewData { 
Id = id, 
FirstName = firstName, 
LastName = lastName, 
Member =  isMember
}; 

memberViewData.Results = _dataRepository.GetMember(search); 

return View("Search", memberViewData); 

}

Am I over thinking this and really should just pass the values to the data access layer and populate the ViewData in the controller, or is there a much more elegant pattern or practise I could use?

Sorry if this seems dump, not allot of people to bounce ideas off and time to dig into the framework.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奈何桥上唱咆哮 2024-07-29 03:06:24

使用modelbinder绑定数据

Use modelbinder to bind data

落叶缤纷 2024-07-29 03:06:24

根据您的代码片段,MemberViewData 类除了具有 Search 类的属性外,还具有 Results 属性。 因此,第一步是使 MemberViewData 从 Search 派生并定义一个构造函数,该构造函数接受 Search 实例作为参数并从中分配其基本属性。 接下来我将更改操作方法,如下所示:

public ActionResult Filter(Search search)  
{ 
    return View("Search", new MemberViewData(search) 
    {
        Results = _dataRepository.GetMember(search)
    }); 
}

According to your snippet MemberViewData class has the Results property in addition to properties of Search class. So first step would be to make MemberViewData derive from Search and define a constructor that accepts Search instance as parameter and assigns it's basic properties from it. Next I would change the action method like so:

public ActionResult Filter(Search search)  
{ 
    return View("Search", new MemberViewData(search) 
    {
        Results = _dataRepository.GetMember(search)
    }); 
}
债姬 2024-07-29 03:06:24

正如 Tadeusz 提到的,ModelBinder 可以帮助您构建 MemberViewData,这将只留下要获取的结果。

您还可以决定创建一个表示服务,该服务了解如何构建此视图数据对象并简单地委托给它。 不过,我更喜欢这里的模型绑定器方法。

Like Tadeusz mentioned, a ModelBinder can help build the MemberViewData for you, which would leave only the results to be fetched.

You could also decide on creating a presentation service that understands how to build this view data object and simply delegate to it. I'd prefer the model binder approach here though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文