Asp.net MVC GridView edit columns option
Asked Answered
S

1

1

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?

Spermaceti answered 4/4, 2011 at 20:53 Comment(0)
L
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.

Lorolla answered 5/4, 2011 at 12:54 Comment(2)
How would I know what equality I check here, Model.data is List of objects type "search" with different properties and it looks like item would just one object, how can I compare to find which one I am looking for in that list? I am thoroughly confused now :(. Can you throw some light on this .action method and how to associate it with rows here?Spermaceti
@sharma I've updated my answer. Hopefully that makes things clear.Lorolla

© 2022 - 2024 — McMap. All rights reserved.