Asp.net MVC GridView edit columns option

让人想犯罪 __ 提交于 2019-12-11 06:18:52

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/5544217/asp-net-mvc-gridview-edit-columns-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!