Abhishek gupta
Abhishek gupta

Reputation: 463

Html page doesn't post to a controller

I have class in a model Customer

When I am clicking a submit button on Cshtml page,

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Registration</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Fname)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Fname)
            @Html.ValidationMessageFor(model => model.Fname)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Lname)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Lname)
            @Html.ValidationMessageFor(model => model.Lname)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Address)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Address)
            @Html.ValidationMessageFor(model => model.Address)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.phoneNo)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.phoneNo)
            @Html.ValidationMessageFor(model => model.phoneNo)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Username)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Username)
            @Html.ValidationMessageFor(model => model.Username)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.ConfirmPassword)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ConfirmPassword)
            @Html.ValidationMessageFor(model => model.ConfirmPassword)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

The page doesn't post to the controller

[HttpPost]
public ViewResult DisplayCustomer(FormCollection Collection)
{
     objinfo.Fname = Request.Form["Fname"].ToString();
     objinfo.Lname = Request.Form["Lname"].ToString();
     objinfo.Address = Request.Form["Address"].ToString();
     objinfo.phoneNo = Request.Form["PhoneNo"].ToString();
     objinfo.Username = Request.Form["UserName"].ToString();
     objinfo.Password = Request.Form["Password"].ToString();
     objutility.InsertEmployee(objinfo);    

     return View("DisplayCustomer");
}

I am not able to get Values into request.form. What particular code I am missing?

Upvotes: 0

Views: 84

Answers (1)

Segev -CJ- Shmueli
Segev -CJ- Shmueli

Reputation: 1621

1 - you don't need to use Request.Form, or FormCollection .

Your controller(s) should look like this:

// this method should just display the page, not handle the post back
public ViewResult DisplayCustomer(){
 return View(); 
}

// this method will handle the post back
[HttpPost]
public ViewResult DisplayCustomer(Customer model){
    // Handle the post back.

    if(ModelState.IsValid){
         /* Hanlde the submission, at this point "model" should have all the properties, such as model.Fname, model.Lname, etc... 

         DB.InserOnSubmit(model);
         DB.Customers.AddObject(model);
         DB.Customers.Add(model);
         you get the point 
         */
    }

}

Upvotes: 1

Related Questions