Одна модель и несколько видов - PullRequest
0 голосов
/ 23 октября 2019

У меня есть одна модель с Mymodel, которая содержит такие свойства, как name, age, contact, phone, address, sortcode, sysmcod e, как это, и все это требуетсяполя. У меня есть контроллер с именем Home.

В HomeController у меня есть методы действия, такие как

  1. index

  2. Контакт (модель Mymodel)

  3. Код (модель Mymodel)

Я перешел на страницу индекса и предоставил подробную информацию и отправил ее на страницу контакта.

при загрузке страницы контактов отображается сообщение об ошибке проверки.

1 Ответ

0 голосов
/ 24 октября 2019

Попробуйте этот пример кода для справки. Я тестировал и работает нормально.

Модель класса:

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

Действия Home Controller:

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>

Проверки работают нормально. Надеюсь это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...