在剃刀的一个视图中使用两个模型

时间:2011-06-18 15:08:33

标签: jquery asp.net-mvc-3 razor

我有一个视图,我需要使用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);
        }

    }

命名空间Szamam.Models {     公共类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>();
}

}

还有一个观点

@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>
}

在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>

在这部分中,我想要另一个模型 - TagModel。

这是我用剃须刀度过的第一个漫长的一天,对于那些愚蠢的问题感到遗憾:/

我在stackoverflow上看到过有关它的其他问题,但我认为这些答案对我没有帮助。

我可以在运行此页面时更改哪些内容?

2 个答案:

答案 0 :(得分:6)

您应该创建一个具有QuestionModel和TagModel属性的视图模型(例如.QuestionTagViewModel)。然后将QuestionTagViewModel传递给您要在其中使用它的视图。

<强>更新

实施例

public class QuestionTagViewModel {
    public QuestionModel {get;set;}
    public TagModel {get;set;}
}

您将能够像这样使用它:

Model.QuestionModel.Content

Model.TagModel.Name

点击此处查看示例:Viewmodels

答案 1 :(得分:5)

或者您可以将元组传递给控制器​​,如此

return View(new Tuple<QuestionModel, TagModel>(){...});