简体   繁体   English

Html.EditorFor-如何根据post参数使用

[英]Html.EditorFor - How to use based on post parameter

My ActionResult looks like this: 我的ActionResult看起来像这样:

public ActionResult Index(string engineer, string contactName, 
                          string company,  string searchKeyword)
{

It then returns a view based on a few LINQ queries running underneath, which use the passed in parameters (engiener, contactName etc) to determine the returned model. 然后,它基于下面运行的一些LINQ查询返回一个视图,这些查询使用传入的参数(engiener,contactName等)确定返回的模型。

I am generating a form in the view which acts as a form to post values to the controller as to filter the results. 我在视图中生成一个表单,该表单用作将值发布到控制器以过滤结果的表单。 At the moment it looks like this - the code works fine and the search boxes work but this seems like bad practice and I can't add a class in to Html.Editor: 目前看起来像这样-代码可以正常工作并且搜索框也可以工作,但这似乎是一种不好的做法,我无法在Html.Editor中添加类:

<div class="form-group">
        <div class="editor-label">
            @Html.Label("Contact Name")
        </div>
        <div class="editor-field">
            @Html.Editor("contactName")
        </div>
    </div>
    <div class="form-group">
        <div class="editor-label">
            @Html.Label("Keyword Search")
        </div>
        <div class="editor-field">
            @Html.Editor("searchKeyword")
        </div>
    </div>

I want to be able to use an EditorFor as that seems best practice but as the parameters are not in the model I am not sure how to reference them in the first part of the statement? 我希望能够使用EditorFor,因为这似乎是最佳实践,但是由于参数不在模型中,因此我不确定如何在语句的第一部分中引用它们?

@Html.EditorFor(model => what Do I put here!?)

You need to have those properties in the model, that's the point of this framework. 您需要在模型中具有这些属性,这就是该框架的重点。

What you might not realize, though, is that the model you pass to the page (let's call it "view model") doesn't have to be the same as the one you store in the database (the "domain model"). 但是,您可能没有意识到,传递给页面的模型(称为“视图模型”)不必与数据库中存储的模型(“域模型”)相同。 The view model needs to have everything that the controller needs to pass from and to the view. 视图模型需要具有控制器需要从视图传递到视图的所有内容。 The domain model needs to have everything that the controller needs to pass from and to the database. 域模型需要具有控制器需要从数据库传递到数据库的所有内容。

While these may happen to be the same thing in trivial scenarios (like plain CRUD applications), if it doesn't work for you, don't hesitate to change them. 尽管在琐碎的场景中(例如普通的CRUD应用程序)这些可能恰好是同一回事,但是如果对您不起作用,请立即进行更改。 Pass to the view a (new) kind of model that contains the parameters you need. 将包含所需参数的一种(新)模型传递给视图。

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

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