开发者

Using two models at one view with razor

I have a view where I need use 2 models because I'm trying to autocmplete box. View is to add a question like at stackoverflow. You must input content, and select tags from autocomplete.

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);
        }

    }

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>();
}

}

And there is a view

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

Of cource there i a problem :

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

In this part I want to have another model - TagModel.

This is my first long day with razor so sorry for those stupid questions :/

I've seen other questions about that at stackoverflow, but I think that answers aren't helpful for me.

What can I change at view to run this page?


You should create a view model (eg.QuestionTagViewModel) which has QuestionModel and TagModel properties. Then pass the QuestionTagViewModel to the view you want to use it in.

Update:

Example

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

You will be able to use it like this:

Model.QuestionModel.Content

and:

Model.TagModel.Name

Look here for an example: Viewmodels


Or you can pass a Tuple to View from Controller like this

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜