locked
MVC rejecting my ViewModel RRS feed

  • Question

  • User323983933 posted

    to be fair, I have never created a ViewModel before, but I have created a lot of classes.

    I created a class.  It has properties and a list of classes.

    It has a constructor.

    It is named \ViewModels\FindHost.cs

    I created a cshtml page and put this at the top:

    @model CountryCousins.ViewModels.FindHost;

    This give a red squiggly and the message: 'FindHost' is a type which is not valid in the given context.

    Is there something I need to do to my class (even name it differently) to MVC-ify it?

    I did NOT name the cs file FindHostViewModel, could that be it?

    Saturday, December 8, 2018 12:11 AM

All replies

  • User1120430333 posted
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using Microsoft.AspNetCore.Mvc.Rendering;
    
    namespace ProgMgmntCore2MVC.Models
    {
        public class TaskViewModels
        {
            public class TaskCreate
            {
                public int TaskId { get; set; }
    
                [Required(ErrorMessage = "Task Name is required")]
                [StringLength(50)]
                public string TaskName { get; set; }
    
                [StringLength(2000)]
                public string Note { get; set; }
    
                [Required(ErrorMessage = "Start Date is required")]
                [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM-dd-yyyy hh:mm:ss tt}")]
                public DateTime? StartDate { get; set; }
                public string ResourceId { get; set; }
                public int ProjectId { get; set; }
                public string TaskDuration { get; set; }
                public string Status { get; set; }
    
                public List<SelectListItem> Statuses { get; set; }
                public List<SelectListItem> Durations { get; set; }
                public List<SelectListItem> Resources { get; set; }
            }
    
            public class TaskEdit
            {
                public int TaskId { get; set; }
    
                [Required(ErrorMessage = "Task Name is required")]
                [StringLength(50)]
                public string TaskName { get; set; }
    
                [StringLength(2000)]
                public string Note { get; set; }
    
                [Required(ErrorMessage = "Start Date is required")]
                [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM-dd-yyyy hh:mm:ss tt}")]
                public DateTime? StartDate { get; set; }
               
                [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM-dd-yyyy hh:mm:ss tt}")]
                public DateTime? EndDate { get; set; }
    
                public string ResourceId { get; set; }
                public int ProjectId { get; set; }
                public string TaskDuration { get; set; }
                public string TaskSpent { get; set; }
                public string Status { get; set; }
    
                public List<SelectListItem> Statuses { get; set; }
                public List<SelectListItem> Durations { get; set; }
                public List<SelectListItem> Resources { get; set; }
                public List<SelectListItem> Spents { get; set; }
            }
            public List<TaskEdit> Tasks { get; set; }
        }
    }
    @model TaskViewModels.TaskCreate
    
    <!DOCTYPE html>
    
    <style type="text/css">
        .editor-field > label {
            float: left;
            width: 150px;
        }
        txtbox                        
        {
            font-family:Arial, Helvetica, sans-serif;
            font-size:12px;
            background: white;
            color:black;
            cursor:text;
            border-bottom: 1px solid #104A7B;
            border-right: 1px solid #104A7B;
            border-left: 1px solid #104A7B;
            border-top:1px solid #104A7B;
            padding-top:10px;       
        }       
    </style>
    
    <html>
    <head>
        <title>Create</title>
    </head>
    
    <body>
        <h1>Task</h1>
    
        @using (Html.BeginForm())
        {
            @Html.ValidationSummary(false, "", new { @class = "text-danger" })
    
        <fieldset>
    
            <legend>Create</legend>
    
            <div class="editor-field">
                @Html.Label("Task Name:")
                @Html.TextBoxFor(model => model.TaskName, new { @Cssclass = "txtbox" })
                @Html.ValidationMessageFor(model => model.TaskName)
            </div>
            <br/>
            <div class="editor-field">
                @Html.Label("Note:")
                @Html.TextAreaFor(model => model.Note)
                @Html.ValidationMessageFor(model => model.Note)
            </div>
            <br/>
            <div class="editor-field">
                @Html.Label("Start Date:")
                @Html.TextBoxFor(model => model.StartDate, new { @Cssclass = "txtbox" } )
                @Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
            </div>
            <br/>
            <div class="editor-field">
                @Html.Label("Duration:")
                @Html.DropDownList("ddlDurations", Model.Durations, "Select...")
            </div>
            <br/>
            <div class="editor-field">
                @Html.Label("Resource:")
                @Html.DropDownList("ddlResources", Model.Resources, "Select...")
            </div>
            <br/>
            <div class="editor-field">
                @Html.Label("Status:")
                @Html.DropDownList("ddlStatusTypes", Model.Statuses, "Select...")
            </div>
            <br/>
            <p>
                <input type="submit" name="submit" value="Save" />
                <input type="submit" name="submit" value="Cancel" />
            </p>
    
        </fieldset>
        }
    
    </body>
    </html>
    
    
    
    @model TaskViewModels.TaskEdit
    
    
    <!DOCTYPE html>
    
    <style type="text/css">
        .editor-field > label {
            float: left;
            width: 150px;
        }
        .txtbox                        
        {
            font-family:Arial, Helvetica, sans-serif;
            font-size:12px;
            background: white;
            color:black;
            cursor:text;
            border-bottom: 1px solid #104A7B;
            border-right: 1px solid #104A7B;
            border-left: 1px solid #104A7B;
            border-top:1px solid #104A7B;
            padding-top:10px;       
        }       
    </style>
    
    <html>
    <head>
        <title>Edit</title>
    </head>
    
    <body>
        <h1>Task</h1>
    
        @using (Html.BeginForm())
        {
    
            @Html.ValidationSummary(false, "", new { @class = "text-danger" })
    
        <fieldset>
    
            <legend>Edit</legend>
    
            @Html.HiddenFor(model => model.TaskId)
    
            <div class="editor-field">
                @Html.Label("Task Name:")
                @Html.TextBoxFor(model => model.TaskName, "", new { @Cssclass = "txtbox" })
                @Html.ValidationMessageFor(model => model.TaskName)
            </div>
            <br/>
            <div class="editor-field">
                @Html.Label("Notes:")
                @Html.TextAreaFor(model => model.Note)
                @Html.ValidationMessageFor(model => model.Note)
            </div>
            <br/>
            <div class="editor-field">
                @Html.Label("Start Date:")
                @Html.TextBoxFor(model => model.StartDate, new { @Cssclass = "txtbox" })
                @Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
             </div>
            <br/>
            <div class="editor-field">
                @Html.Label("End Date:")
                @Html.TextBoxFor(model => model.EndDate, new { @Cssclass = "txtbox" })
                @Html.ValidationMessageFor(model => model.EndDate, "", new { @class = "text-danger" })
             </div>
            <br/>
            <div class="editor-field">
                @Html.Label("Duration:")
                @Html.DropDownListFor(model => model.TaskDuration, Model.Durations)
            </div>
            <br/>
            <div class="editor-field">
                @Html.Label("Spent:")
                @Html.DropDownListFor(model => model.TaskSpent, Model.Spents)
            </div>
            <br/>
            <div class="editor-field">
                @Html.Label("Resource:")
                @Html.DropDownListFor(model => model.ResourceId, Model.Resources)
            </div>
            <br/>
            <div class="editor-field">
                @Html.Label("Status:")
                @Html.DropDownListFor(model => model.Status, Model.Statuses)
            </div>
            <br/>
            <p>
                <input type="submit" name="submit" value="Save" />
                <input type="submit" name="submit" value="Cancel" />
            </p>
    
        </fieldset>
        }
    
    </body>
    </html>
    
    
    
    
    
    
    

    Saturday, December 8, 2018 12:38 PM
  • User1520731567 posted

    Hi some_yahoo,

    Actually, There is no difference between reference ViewModel and reference Mode.

    For example:

    in \ViewModels\FindHost.cs

    namespace TestApplication1.ViewModels
    {
        public class FindHost
        {
            ....
           
        }
        public class FindHostViewModel
        {
            ...
        }
    }

    In Razor:

    Remove the semi-colon from the end of your model declaration.

    @model <namespace>.<fileName>.className

    if you would like to use FindHostViewModel with single row data:

    @model TestApplication1.ViewModels.FindHostViewModel

    if you would like to use FindHostViewModel with multi-line data:

    @model List<TestApplication1.ViewModels.FindHostViewModel>

    If you have any questions,please post more details.

    Best Regards.

    Yuki Tao

    Monday, December 10, 2018 8:50 AM