开发者

How to use DataContract in MVC 3.0 Razor

开发者 https://www.devze.com 2023-03-13 16:55 出处:网络
I have a simple view: @model Szamam.Models.Question @{ ViewBag.Title = \"Display\"; Layout = \"~/Views/Shared/_Layout.cshtml\";

I have a simple view:

@model Szamam.Models.Question

@{
    ViewBag.Title = "Display";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Display</h2>

<fieldset>
    <legend>Question</legend>

    <div class="display-label">Content</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Content)
    </div>

    <div class="display-label">CreationDate</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.CreationDate)
    </div>

    <div class="display-label">nickname</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Creator.NickName)
    </div>
</fieldset>

model class:

using System;

namespace Szamam.Models
{
    public class Question
    {
        private string _content;

        public string Content
        {
            get { return _content; }
            set { _content = value; }
        }

        public User Creator
        {
            get { return _creator; }
            set { _creator = value; }
        }

        public DateTime CreationDate
        {
            get { return _creationDate; }
            set { _creationDate = value; } 
        }

        private User _creator;

        private DateTime _creationDate;
    }
}

And my controller

namespace Szamam.开发者_Go百科Controllers
{
    public class QuestionController : Controller
    {
        //
        // GET: /Question/

        public ActionResult Display()
        {
            return
                View(new Question {Content = "someContent", Creator = new User {NickName = "someNickName"}, CreationDate = DateTime.Now});
        }

    }
}

Now I need use wcf service and model class I get from it so the question is :

Do I need model classes in asp.net mvc project if I will have it from wcf service?

Or I need some converter from datacontract to model class at webservice?

I will be pleased if you would give me some example

Best regards


You could use the objects from your WCF service right away but I wouldn't recommend that. I would use a ViewModel for your views and also abstract away you connection to your WCF service.

0

精彩评论

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

关注公众号