尝试将模型添加到视图但不显示

时间:2016-02-07 22:24:37

标签: asp.net-mvc razor

为什么我在图像上找不到模型:

enter image description here

ContactViewModel类是:

using System.ComponentModel.DataAnnotations;

namespace Moran.ViewModels
{
    public class ContactViewModel
    {
        [Required]
        [StringLength(255, MinimumLength = 5)]
        public string Name { get; set; }
        [Required]
        [EmailAddress]
        public string Email { get; set; }
        [Required]
        [StringLength(1024, MinimumLength = 5)]
        public string Message { get; set; }
    }
}

Contact.cshtml:

@*
    For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
*@
@model BazarMoran.ViewModels.ContactViewModel
@{
    ViewBag.Title = "Contact us";
}

@section Scripts {
    <script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
}

<div class="row">
    <div class="col-md-6 col-md-offset-3">
        <!--Leave the form in the middle of the page-->
        <h2>Contact us</h2>


        <form method="post">

            <div>
                <div asp-validation-summary="ValidationSummary.ModelOnly"></div>
                <div class="form-group">
                    <label asp-for="Name">Name</label>
                    <input class="form-control" asp-for="Name" />
                    <span asp-validation-for="Name" class="text-muted"></span>
                </div>
            </div>
            <div>
                <div class="form-group">
                    <label asp-for="Email">Email</label>
                    <input class="form-control" type="email" asp-for="Email" />
                    <span asp-validation-for="Email" class="text-muted"></span>
                </div>
            </div>

            <div class="form-group">
                <label asp-for="Message">Message</label>
                <textarea class="form-control" cols="40" rows="4" asp-for="Message"></textarea>
                <span asp-validation-for="Message" class="text-muted"></span>
            </div>

            <div class="form-group">
                <a asp-controller="App" asp-action="Index" class="btn btn-default">Cancel</a>
                <input type="submit" value="send Message" class="btn btn-success" />
                <!--With in a button we can use bootstrap to make it nice using font-awesome
                    Added at the bower.json file
                    <i class="fa fa-angle-left></i>-->
            </div>
            <!--This will show the message received on this case if the mail was sent correctly-->
            @if (ViewBag.Message != null)
            {
                <div>@ViewBag.Message</div>

            }

        </form>
    </div> <!--End class col-->
</div> <!--End class row-->

行:@model BazarMoran.ViewModels.ContactViewModel无法识别。

提前谢谢

1 个答案:

答案 0 :(得分:1)

您只是使用了错误的命名空间。

您的ViewModel被定义为Moran.ViewModels命名空间而不是BazarMoran.ViewModels

然后你必须使用

@model Moran.ViewModels.ContactViewModel 

而不是

@model BazarMoran.ViewModels.ContactViewModel

或者只是将名称空间更改为ContactViewModel班级为BazarMoran.ViewModels