Wednesday, 7 January 2015

How to pass data from View to Controller in MVC4 Razor using C#/VB.NET

Hi friends,in this article I will explain about How to pass data from View to Controller in MVC4 Razor using C#/VB.NET.
I already explained in the previous articles about MVC4 Razor: How to compare password and confirm password in ASP.NET MVC4 RazorHow to create a DropDownList field for MVC4 Razor view? and How to Update multiple rows at once Using MVC 4 Razor and Entity Framework


Controller:
    public ActionResult Register()
        {
            var obj=new Home();
            return View(obj);
        }
        [HttpPost]
        public ActionResult Register(Home obj1)
        {
            obj1.Username = Request.Form["txtUsername"].ToString();
            obj1.Password = Request.Form["txtpwd"].ToString();
            int retval = obj1.Register();
            return View("Register", obj1);
        }


View:
@model MvcNew.Models.Home

@{
    ViewBag.Title = "Register";
}

<h2>Register</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
   
    <fieldset>
        <legend>Home</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Username)
        </div>
        <div class="editor-field">
             @Html.TextBox("txtUsername")
            @Html.ValidationMessageFor(model => model.Username)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div class="editor-field">
           @Html.TextBox("txtpwd")
            @Html.ValidationMessageFor(model => model.Password)
        </div>

        <p>
            <input type="submit" value="Create"  />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

1 comment:

  1. well this was very nice article and very good information for ASP.NET learners i was really satisfied see this information we also provide ASP.NET Online Training

    ReplyDelete

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.