如何在 mvc actionresult 中获取 post 标签
how to get post tags in mvc actionresult
我有这个 ViewModel:
public string Title { get; set; }
public string Description { get; set; }
public int City { get; set; }
并且在视图中,用户可以像这样添加一些标签:
book,story,c#,...
UL
中 Li
中可见的所有标签。
如何在 ActionResult 中获取 All 标签值。
我搜索 Google 但找不到任何结果。
我认为在模型中添加一个 List
并且所有 li 名称都相同?做对了吗?
我的 html 是这样的:
<li class="tag-item">one</li>
<li class="tag-item">two</li>
<li class="tag-item">three</li>
更新:
我使用此脚本将标签添加到 UL
:
$("#txttag").keydown(function (e) {
var value = $(this).val();
if (value.length >0 && e.which==13)
{
var markup = "<li class='tag-item'> @Html.HiddenFor(x => x.Tags)" + value + "<a class='tag-close'>X</a></li>";
$("#taglist").append(markup);
$(this).val('');
}
});
但在 Li 值中显示 @Html.HiddenFor(x => x.Tags)
,
您需要 post 操作结果的值。将列表添加到您的模型中:
public string Title { get; set; }
public string Description { get; set; }
public int City { get; set; }
public List<string> Tags { get; set; }
更改 Javascript 代码以包含隐藏 input
字段中的值:
var markup = "<li class='tag-item'><input type='hidden' name='Tags' value='" + value + "' />" + value + "<a class='tag-close'>X</a></li>";
您的代码不会设置隐藏字段的 value=""
参数,但使用上面的代码会。
我有这个 ViewModel:
public string Title { get; set; }
public string Description { get; set; }
public int City { get; set; }
并且在视图中,用户可以像这样添加一些标签:
book,story,c#,...
UL
中 Li
中可见的所有标签。
如何在 ActionResult 中获取 All 标签值。
我搜索 Google 但找不到任何结果。
我认为在模型中添加一个 List
并且所有 li 名称都相同?做对了吗?
我的 html 是这样的:
<li class="tag-item">one</li>
<li class="tag-item">two</li>
<li class="tag-item">three</li>
更新:
我使用此脚本将标签添加到 UL
:
$("#txttag").keydown(function (e) {
var value = $(this).val();
if (value.length >0 && e.which==13)
{
var markup = "<li class='tag-item'> @Html.HiddenFor(x => x.Tags)" + value + "<a class='tag-close'>X</a></li>";
$("#taglist").append(markup);
$(this).val('');
}
});
但在 Li 值中显示 @Html.HiddenFor(x => x.Tags)
,
您需要 post 操作结果的值。将列表添加到您的模型中:
public string Title { get; set; }
public string Description { get; set; }
public int City { get; set; }
public List<string> Tags { get; set; }
更改 Javascript 代码以包含隐藏 input
字段中的值:
var markup = "<li class='tag-item'><input type='hidden' name='Tags' value='" + value + "' />" + value + "<a class='tag-close'>X</a></li>";
您的代码不会设置隐藏字段的 value=""
参数,但使用上面的代码会。