1

我有这样的看法:

<%= 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 的新手,任何人都可以指导我从这里去哪里,有什么建议吗?

4

1 回答 1

1

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

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

[更新]

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

于 2011-04-05T12:54:12.763 回答