Asp.net MVC GridView的编辑列选项 [英] Asp.net MVC GridView edit columns option

查看:625
本文介绍了Asp.net MVC GridView的编辑列选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的观点:

<%= 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的从编辑按钮,我针对/搜索/编辑页面,我知道我需要在控制器中的一些code表示//查找/编辑,但我没有得到的文本框中在那里我可以编辑结果单元格中的文本。我是新来的MVC任何人都可以直接我在哪里,我要在这里,有什么建​​议?

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?

推荐答案

这很可能比较始终返回false:项目=&GT; (Model.data ==项目)
这将prevent被显示的编辑框。

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

尝试重写比较简单值之间的比较(例如ID的)或实施的等于你的数据类和使用,在==代替

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 ==

[更新]

的比较来决定哪些行(S)应显示在编辑模式下,其中真正表示呈现在编辑模式下排。

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

比如说你要编辑对应于一个项目给定ID的行。然后,您的比较将类似于此项目=&GT; item.Id == Model.SelectedRowId

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);
}

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

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

在一个侧面说明,我建议你不要让你的视图模型加载在的getData()方法将其自己的数据。视图模型应该是没有什么比你使用将数据从控制器传输到您的视图的容器了。将数据放入一个视图模型是控制器的责任。

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.

这篇关于Asp.net MVC GridView的编辑列选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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