【问题标题】:Single model and multiple views单个模型和多个视图
【发布时间】:2020-02-19 15:05:36
【问题描述】:

我有一个带有 Mymodel 的模型,它包含像 nameagecontactphoneaddresssortcodesysmcode 这样的属性,所有这些都是必需的字段。 我有一个名为 Home 的控制器。

在 HomeController 我有类似的操作方法

  1. 索引

  2. 联系方式(Mymodel 模型)

  3. 代码(Mymodel 模型)

我已经导航到索引页面并提供了详细信息并提交,它导航到联系人页面。

在加载联系页面时,它会显示验证错误消息。

【问题讨论】:

  • 您能提供代码吗?如果我们能看到上下文,会更容易提供帮助。

标签: model-view-controller view model


【解决方案1】:

试试这个示例代码供您参考。我测试并工作正常。

模型类:

public class Mymodel
    {
        [Required]
        public string Name { get; set; }
        [Required]
        public string Age { get; set; }
        [Required]
        public string Contact { get; set; }
        [Required]
        public string Phone { get; set; }
        [Required]
        public string Address { get; set; }
        [Required]
        public string Sortcode { get; set; }
        [Required]
        public string Sysmcode { get; set; }
    }

主页控制器操作:

public class HomeController : Controller
    {
        public ActionResult Index()
        {
            //assigned value for test purposes. 
            Mymodel tt = new Mymodel()
            {
                Name = "Dumm",
                Age = "55",
                Address = "test",
                Contact = "46516",
                Phone = "516566",
                Sortcode = "sdad",
                Sysmcode = "asdad"
            };

            return View(tt);
        }

       public ActionResult Contact(Mymodel model)
        {
            ViewBag.Message = "Your contact page.";

            return View(model);
        }
}

最后是视图:

索引.cshtml

@model WebApplication1.Models.Mymodel

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")


@using (Html.BeginForm("Contact", "Home", FormMethod.Post)) 
{
                @Html.AntiForgeryToken()

                <div class="form-horizontal">
        <h4>Mymodel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Age, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Contact, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Contact, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Contact, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Sortcode, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Sortcode, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Sortcode, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Sysmcode, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Sysmcode, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Sysmcode, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

Contact.cshtml

@model WebApplication1.Models.Mymodel

<div>
    <h4>Mymodel</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Name)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Name)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Age)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Age)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Contact)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Contact)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Phone)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Phone)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Address)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Address)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Sortcode)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Sortcode)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Sysmcode)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Sysmcode)
        </dd>

    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
    @Html.ActionLink("Back to List", "Index")
</p>

验证工作正常。 希望这可以帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多