单个模型和多个视图

问题描述 投票:0回答:1

我有一个带有Mymodel的模型,它包含像nameagecontactphoneaddresssortcodesysmcod e之类的属性,所有这些都是必填字段。我有名称为Home的控制器。

在HomeController中,我有类似的操作方法

  1. 索引

  2. Contact(Mymodel model)

  3. Code(Mymodel模型)

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

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

model-view-controller view model
1个回答
0
投票
Model Class:

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

最后是视图:

Index.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>

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

© www.soinside.com 2019 - 2024. All rights reserved.