Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • cshtml file in MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 128
    Comment on it

    In MVC projects you have to create views inside controller class for performing tasks.

     

    While creating views you also have to create their HTML designs for separate views in a single controller or in different controllers.

     

    So what you do is first create view definition in the controller class

     

    public ActionResult Index()
            {
                try
                {
    
                    Details objectAssignmentModel = new Details();
    
                    objectAssignmentModel.Category = new SelectList(Enum.GetValues(typeof(Common.Category)).Cast<Common.Category>().Select(v => new SelectListItem
                    {
                        Value = v.ToString(),
                        Text = ((int)v).ToString()
                    }).ToList(), "Value", "Text");
    
                    objectAssignmentModel.Country = new SelectList(Enum.GetValues(typeof(Common.Country)).Cast<Common.Country>().Select(v => new SelectListItem
                    {
                        Text = v.ToString(),
                        Value = ((int)v).ToString()
                    }).ToList(), "Value", "Text");
    
                    objectAssignmentModel.Hobbies = new SelectList(Enum.GetValues(typeof(Common.Hobbies)).Cast<Common.Hobbies>().Select(v => new SelectListItem
                    {
                        Text = v.ToString(),
                        Value = ((int)v).ToString()
                    }).ToList(), "Value", "Text");
    
    
    
    
                    return View(objectAssignmentModel);
                }
                catch (Exception)
                {
    
                    throw;
                }
    
    
    
            }
    

     

    Now HTML needs to be generated which gets displayed when this view gets invoked by the application.

     

    @{
        ViewBag.Title = "Contact";
    }
    <h2>@ViewBag.Title.</h2>
    <h3>@ViewBag.Message</h3>
    
    <address>
        One Microsoft Way<br />
        Redmond, WA 98052-6399<br />
        <abbr title="Phone">P:</abbr>
        425.555.0100
    </address>
    
    <address>
        <strong>Support:</strong>   <a href="mailto:Support@example.com">Support@example.com</a><br />
        <strong>Marketing:</strong> <a href="mailto:Marketing@example.com">Marketing@example.com</a>
    </address>
    

     

    This view contains both the .cs file and the .html file so that's why it is called .cshtml file

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: