编辑文本框上的项目时 MVC2 出现问题
我在 mvc 上有这个模型:
public class User
{
public string Name
{
get;
set;
}
public IList<string>RelatedTags
{
get;
set;
}
}
以及以下类型的视图(用户)来编辑添加用户(AddEdit.aspx 视图):
<div>
<%: Html.LabelFor(e => e.Name)%>
<%: Html.TextBoxFor(e => e.Name)%>
<%: Html.ValidationMessageFor(e => e.Name)%>
</div>
<div>
<%: Html.LabelFor(e => e.RelatedTags)%>
<%: Html.TextBoxFor(e => e.RelatedTags)%>
<%: Html.ValidationMessageFor(e => e.RelatedTags)%>
</div>
另一方面,我有一个“RelatedTags”字段。我需要(在操作控制器端)与我添加的用户相关的标签列表。为此,我创建了一个自定义模型绑定器(以获取文本框的字符串并将其作为列表传递):
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
List<string> listoftags = bindingContext.ValueProvider.GetValue("RelatedTags").AttemptedValue.Split(',').ToList<string>();
return listoftags;
}
实际上我可以使用 AddEdit.aspx 添加新用户(在控制器端我获取相关标签的列表,但是当我编辑用户时,我不知道在哪里可以将此列表转换为逗号字符串,并且我不知道应该更改视图上的这些行:
<div>
<%: Html.LabelFor(e => e.RelatedTags)%>
<%: Html.TextBoxFor(e => e.RelatedTags)%>
<%: Html.ValidationMessageFor(e => e.RelatedTags)%>
</div>
目前,以防万一,我为 IList 创建了一个扩展方法:
public static class Extensions
{
public static string ToCommaString<T>(this IList<T> input)
{
StringBuilder sb = new StringBuilder();
foreach (T value in input)
{
sb.Append(value);
sb.Append(",");
}
return sb.ToString();
}
}
你可以吗当我编辑用户在输入字段中看到用逗号分隔的列表中的所有元素时,给我任何帮助吗?
提前非常感谢
何塞
。
i have this model on mvc:
public class User
{
public string Name
{
get;
set;
}
public IList<string>RelatedTags
{
get;
set;
}
}
And the following typed view (user) to edit an add a user (AddEdit.aspx view):
<div>
<%: Html.LabelFor(e => e.Name)%>
<%: Html.TextBoxFor(e => e.Name)%>
<%: Html.ValidationMessageFor(e => e.Name)%>
</div>
<div>
<%: Html.LabelFor(e => e.RelatedTags)%>
<%: Html.TextBoxFor(e => e.RelatedTags)%>
<%: Html.ValidationMessageFor(e => e.RelatedTags)%>
</div>
For other hand i have a "RelatedTags" field. I needed (on action controller side) a List of tags related with the user that im adding. For this reason i created a Custom model binder (to take the string of the textbox and pass it as List):
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
List<string> listoftags = bindingContext.ValueProvider.GetValue("RelatedTags").AttemptedValue.Split(',').ToList<string>();
return listoftags;
}
Actually i can use the AddEdit.aspx to add a new user (on controller side im getting a List of related tags, but when i edit a user i dont know where i can convert this List to comma string, and either i dont know should change these lines on the view:
<div>
<%: Html.LabelFor(e => e.RelatedTags)%>
<%: Html.TextBoxFor(e => e.RelatedTags)%>
<%: Html.ValidationMessageFor(e => e.RelatedTags)%>
</div>
By the moment, just in case, i created an extension method for the IList:
public static class Extensions
{
public static string ToCommaString<T>(this IList<T> input)
{
StringBuilder sb = new StringBuilder();
foreach (T value in input)
{
sb.Append(value);
sb.Append(",");
}
return sb.ToString();
}
}
Could you give me any help for when im editing a user see in the input field a string separated with commas with all the elements of the list?
Thanks a lot in advance.
Best Regards.
Jose.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许你可以使用这个模型?
不需要粘合剂和延伸方法。
Maybe you could use this model?
No binders and extension methods required.