C# Asp.net MVC GridView编辑列选项

C# Asp.net MVC GridView编辑列选项,c#,asp.net-mvc-2,gridview,mvccontrib,mvccontrib-grid,C#,Asp.net Mvc 2,Gridview,Mvccontrib,Mvccontrib Grid,我认为: <%= 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 => (Mode

我认为:

<%= 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)
{
column.For(x=>x.results)
.Action(item=>Html.ActionLink(item.results,“Edit”).ToString(),
item=>Html.TextBox(“result”,item.results).ToString(),
item=>(Model.data==item))
.命名为(“结果”);
column.For(x=>x.refId)
.命名(“参考ID”);
column.For(x=>x.fileLocation)
.命名(“文件位置”);
})
.Attributes(样式=>宽度:100%,边框=>1)
控制器看起来像:

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

        StringSearchResultsModelIndex modelInstance = new StringSearchResultsModelIndex();
        modelInstance.getData();
         return View("SearchGUIString", modelInstance);
      }
public ActionResult Index()
{
//i数不清的结果;
StringSearchResultsModelIndex modelInstance=新的StringSearchResultsModelIndex();
modelInstance.getData();
返回视图(“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; } }

}
公共类StringSearchResultsModelIndex:IStringSearchResultsModelIndex
{
私有IEnumerable m_数据;
私有字符串id;
公共IEnumerable getData()
{
列表模型=新列表();
StringSearchResultModel_sModel=新StringSearchResultModel();
对于(int i=1;i<11;i++)
{
_sModel=新的StringSearchResultModel();
_sModel.fileLocation=“Location”+i;
_sModel.refId=“refId”+i;
_sModel.results=“results”+i;
模型。添加(_sModel);
}
m_数据=模型;
收益模型;
}
公共IEnumerable数据{get{return m_data;}set{m_data=value;}}
公共字符串SelectedRowID{get{return id;}set{id=value;}}
}

当我从ActionLink单击编辑按钮时,我被引导到/search/edit页面,我知道我需要在controller中为//search/edit添加一些代码,但我没有得到可以在结果单元格中编辑文本的文本框。我是MVC新手,有人能告诉我接下来该去哪里吗,有什么建议吗?

很可能这个比较总是返回false:
item=>(Model.data==item)
。 这将阻止显示编辑框

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

[更新]

比较用于决定哪些行应在编辑模式下显示,其中
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()
方法中加载自己的数据。视图模型只应是用于将数据从控制器传输到视图的容器。将数据放入视图模型是控制器的责任。

此比较很可能总是返回false:
item=>(model.data==item)
。 这将阻止显示编辑框

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

[更新]

比较用于决定哪些行应在编辑模式下显示,其中
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()
方法中加载自己的数据。视图模型只应是用于将数据从控制器传输到视图的容器。将数据放入视图模型是控制器的责任。

我如何知道我在此处检查的等式,model.data是具有不同属性的对象列表类型“search”,并且看起来该项仅为一个对象,我如何比较以找到我在该列表中查找的对象?我现在完全糊涂了:(.你能解释一下这个.action方法吗?以及如何将它与这里的行关联?@sharma我已经更新了我的答案。希望这能让事情变得清楚。我怎么知道我在这里检查的等式是什么,Model.data是对象列表类型“search”由于属性不同,而且项看起来只是一个对象,我如何比较才能找到列表中要查找的对象呢?我现在完全糊涂了:(.你能解释一下这个。操作方法以及如何将其与此处的行关联吗?@sharma我已更新了我的答案。希望这能让事情变得清楚。