【问题标题】:How to use DataContract in MVC 3.0 Razor如何在 MVC 3.0 Razor 中使用 DataContract
【发布时间】:2023-03-11 03:52:01
【问题描述】:

我有一个简单的看法:

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

模型类:

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

还有我的控制器

namespace Szamam.Controllers
{
    public class QuestionController : Controller
    {
        //
        // GET: /Question/

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

    }
}

现在我需要使用 wcf 服务和从中获得的模型类,所以问题是:

如果我要从 wcf 服务获得模型类,我是否需要在 asp.net mvc 项目中使用模型类?

或者我需要一些从数据合同到网络服务模型类的转换器?

如果你能给我一些例子,我会很高兴

最好的问候

【问题讨论】:

    标签: wcf asp.net-mvc-3


    【解决方案1】:

    您可以立即使用 WCF 服务中的对象,但我不建议这样做。我会为您的视图使用 ViewModel,并抽象出您与 WCF 服务的连接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多