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 中的编辑按钮时,我被定向到 /search/Edit 页面,我知道我需要在控制器中添加一些用于 //search/Edit 的代码但我没有得到可以在结果单元格中编辑文本的文本框。我是 MVC 新手,谁能指导我应该从这里去哪里,有什么建议吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很可能此比较始终返回 false :
item => (模型.数据==项目)
。这将阻止显示编辑框。
尝试将比较重写为简单值(例如 id)之间的比较或实现 Equals< /a> 在您的数据类上,并使用它代替 ==
[Update]
比较用于决定应在编辑模式下显示哪些行,其中
true 表示“在编辑模式下呈现行”。
假设您要编辑与具有给定 ID 的项目对应的行。您的比较将类似于此
item => item.Id == Model.SelectedRowId
。在您的控制器中,您将执行如下操作:
请注意,您需要将
SelectedRowId
属性添加到您的视图模型类中。另外,我建议您不要让视图模型在
getData()
方法中加载它自己的数据。视图模型应该只不过是一个容器,用于将数据从控制器传输到视图。将数据放入视图模型是控制器的责任。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:
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.