Asp.net MVC GridView 编辑列选项

发布于 2024-10-30 19:35:18 字数 2037 浏览 2 评论 0原文

我有这样的视图:

<%= Html.Grid(Model.data).Columns(column => {
column.For(x => x.results)
    .Action(item => Html.ActionLink(item.results,"Edit").ToString(),
        item => Html.TextBox("result",item.results).ToString(),
        item => (Model.data == item))
       .Named("Results");
             column.For(x => x.refId)
                 .Named("Reference ID");
             column.For(x => x.fileLocation)
                 .Named("File Location");

                })
                .Attributes(style => "width:100%", border => 1)

控制器看起来像:

  public ActionResult Index()
       {
        //  IEnumerable<TranslationResults> results;

        StringSearchResultsModelIndex modelInstance = new StringSearchResultsModelIndex();
        modelInstance.getData();
         return View("SearchGUIString", modelInstance);
      }

数据:

 public class StringSearchResultsModelIndex : IStringSearchResultsModelIndex
{

    private IEnumerable<StringSearchResultModel> m_data;
    private string id;

    public IEnumerable<StringSearchResultModel> getData()
    {

        List<StringSearchResultModel> models = new List<StringSearchResultModel>();
        StringSearchResultModel _sModel = new StringSearchResultModel();
        for (int i = 1; i < 11; i++)
        {
            _sModel = new StringSearchResultModel();
            _sModel.fileLocation = "Location" + i;
            _sModel.refId = "refID" + i;
            _sModel.results = "results" + i;
            models.Add(_sModel);

        }
        m_data = models;
        return models;
    }

    public IEnumerable<StringSearchResultModel> data { get { return m_data; } set { m_data = value; } }
    public string SelectedRowID {get {return id ; } set { id = value; } }

}

当我单击 ActionLink 中的编辑按钮时,我被定向到 /search/Edit 页面,我知道我需要在控制器中添加一些用于 //search/Edit 的代码但我没有得到可以在结果单元格中编辑文本的文本框。我是 MVC 新手,谁能指导我应该从这里去哪里,有什么建议吗?

I have the view :

<%= Html.Grid(Model.data).Columns(column => {
column.For(x => x.results)
    .Action(item => Html.ActionLink(item.results,"Edit").ToString(),
        item => Html.TextBox("result",item.results).ToString(),
        item => (Model.data == item))
       .Named("Results");
             column.For(x => x.refId)
                 .Named("Reference ID");
             column.For(x => x.fileLocation)
                 .Named("File Location");

                })
                .Attributes(style => "width:100%", border => 1)

And the controller looks like:

  public ActionResult Index()
       {
        //  IEnumerable<TranslationResults> results;

        StringSearchResultsModelIndex modelInstance = new StringSearchResultsModelIndex();
        modelInstance.getData();
         return View("SearchGUIString", modelInstance);
      }

the data:

 public class StringSearchResultsModelIndex : IStringSearchResultsModelIndex
{

    private IEnumerable<StringSearchResultModel> m_data;
    private string id;

    public IEnumerable<StringSearchResultModel> getData()
    {

        List<StringSearchResultModel> models = new List<StringSearchResultModel>();
        StringSearchResultModel _sModel = new StringSearchResultModel();
        for (int i = 1; i < 11; i++)
        {
            _sModel = new StringSearchResultModel();
            _sModel.fileLocation = "Location" + i;
            _sModel.refId = "refID" + i;
            _sModel.results = "results" + i;
            models.Add(_sModel);

        }
        m_data = models;
        return models;
    }

    public IEnumerable<StringSearchResultModel> data { get { return m_data; } set { m_data = value; } }
    public string SelectedRowID {get {return id ; } set { id = value; } }

}

when I click the edit button from ActionLink, I am directed to /search/Edit page, I understand that I need to have some code in controller for //search/Edit but I am not getting the text box where I can edit the text in result cell. I am new to MVC can anyone direct me where I am should be going from here, any suggestions?

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

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

发布评论

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

评论(1

烟雨凡馨 2024-11-06 19:35:18

很可能此比较始终返回 false : item => (模型.数据==项目)
这将阻止显示编辑框。

尝试将比较重写为简单值(例如 id)之间的比较或实现 Equals< /a> 在您的数据类上,并使用它代替 ==

[Update]

比较用于决定应在编辑模式下显示哪些行,其中 true 表示“在编辑模式下呈现行”。

假设您要编辑与具有给定 ID 的项目对应的行。您的比较将类似于此 item => item.Id == Model.SelectedRowId

在您的控制器中,您将执行如下操作:

public ActionResult Edit(string id)
{
  var model = new StringSearchResultsModelIndex();
  model.getData();
  model.SelectedRowId = id;
  return View("SearchGUIString", model);
}

请注意,您需要将 SelectedRowId 属性添加到您的视图模型类中。

另外,我建议您不要让视图模型在 getData() 方法中加载它自己的数据。视图模型应该只不过是一个容器,用于将数据从控制器传输到视图。将数据放入视图模型是控制器的责任。

Most likely this compare always returns false : item => (Model.data == item).
This will prevent the edit box from being displayed.

Try rewriting the comparison as a compare between simple values (for example id's) or implement Equals on your data class and use that in stead of ==

[Update]

The comparison is used to decide which row(s) should be displayed in edit mode, where true means 'render the row in edit mode'.

Say you want to edit the row that corresponds to an item with a given id. Your comparison would then look similar to this item => item.Id == Model.SelectedRowId.

In your controller you would do something like this:

public ActionResult Edit(string id)
{
  var model = new StringSearchResultsModelIndex();
  model.getData();
  model.SelectedRowId = id;
  return View("SearchGUIString", model);
}

Note that you need to add the SelectedRowId property to your view model class.

On a side note, I'd recommend you do not let your view model load it's own data in the getData() method. A view model should be nothing more than a container you use to transfer data from your controller to your view. Putting data into a view model is the responsibility of the controller.

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