use*_*618 1 jquery razor asp.net-mvc-3
我有一个视图,我需要使用2个模型,因为我正在尝试自动完成框.View是添加类似stackoverflow的问题.您必须输入内容,然后从自动填充中选择标记.
public class QuestionController : Controller
{
public ActionResult Add()
{
return View();
}
[HttpPost]
public ActionResult Add(QuestionModel model)
{
if (ModelState.IsValid)
{
return RedirectToAction("Index", "Home");
}
else
{
return View(model);
}
}
public ActionResult TagName(string q)
{
var tags = new List<TagModel>
{
new TagModel {Name = "aaaa", NumberOfUse = "0"},
new TagModel {Name = "mkoh", NumberOfUse = "1"},
new TagModel {Name = "asdf", NumberOfUse = "2"},
new TagModel {Name = "zxcv", NumberOfUse = "3"},
new TagModel {Name = "qwer", NumberOfUse = "4"},
new TagModel {Name = "tyui", NumberOfUse = "5"},
new TagModel {Name = "asdf[", NumberOfUse = "6"},
new TagModel {Name = "mnbv", NumberOfUse = "7"}
};
var tagNames = (from p in tags where p.Name.Contains(q) select p.Name).Distinct().Take(3);
string content = string.Join<string>("\n", tagNames);
return Content(content);
}
}
Run Code Online (Sandbox Code Playgroud)
namespace Szamam.Models {public class QuestionModel {private string _content;
public string Content
{
get { return _content; }
set { _content = value; }
}
public UserModel Creator
{
get { return _creator; }
set { _creator = value; }
}
public DateTime CreationDate
{
get { return _creationDate; }
set { _creationDate = value; }
}
public List<TagModel> TagModels
{
get { return _tagModels; }
set { _tagModels = value; }
}
private UserModel _creator;
private DateTime _creationDate;
private List<TagModel> _tagModels=new List<TagModel>();
}
Run Code Online (Sandbox Code Playgroud)
}
有一种观点
@model Szamam.Models.QuestionModel
@{
ViewBag.Title = "Add";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
Add</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.autocomplete.js")" type="text/javascript"></script>
<link href="@Url.Content("~/Scripts/jquery.autocomplete.css")" rel="stylesheet" type="text/css" />
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>QuestionModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Content)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Content)
@Html.ValidationMessageFor(model => model.Content)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CreationDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CreationDate)
@Html.ValidationMessageFor(model => model.CreationDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TagModels)
</div>
@*here is a place for autocomplete for tags *@
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name)
</div>
<script type="text/javascript">
$(document).ready(function () {
$("#TagName").autocomplete('@Url.Action("TagName", "Question")', { minChars: 3 });
});
</script>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
Run Code Online (Sandbox Code Playgroud)
cource有一个问题:
@*here is a place for autocomplete for tags *@
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name)
</div>
Run Code Online (Sandbox Code Playgroud)
在这部分我想要另一个模型 - TagModel.
这是我用剃刀做的第一个漫长的一天,对于那些愚蠢的问题感到抱歉:/
我在stackoverflow上看到过关于它的其他问题,但我认为答案对我没有帮助.
我可以在运行此页面时更改哪些内容?
您应该创建一个具有QuestionModel和TagModel属性的视图模型(例如,QuestionTagViewModel).然后将QuestionTagViewModel传递给您要在其中使用的视图.
更新:
例
public class QuestionTagViewModel {
public QuestionModel {get;set;}
public TagModel {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
您将能够像这样使用它:
Model.QuestionModel.Content
Run Code Online (Sandbox Code Playgroud)
和:
Model.TagModel.Name
Run Code Online (Sandbox Code Playgroud)
在这里查看示例:Viewmodels
或者您可以像这样将一个元组传递给控制器
return View(new Tuple<QuestionModel, TagModel>(){...});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5342 次 |
| 最近记录: |