0

I am learning MVC, following THIS tutorial. (link will take you directly to where i'm stuck). so far I have learnt, there's a controller for every view. Now i have to take input from user through web entry form as mentioned in tutorial. In my project, i have a controller named Default1 and i can run it as localhost:xyz/Default1/Index. it runs perfect.

Then i created a new Controller, named Default2 and bound it to some view to display some data, and it worked perfect as localhost:xyz/Default2/Displaycustomer. the customer information was static (hard coded). and controller is as:

    public ViewResult DisplayCustomers()
    {
        Customer cobj = new Customer();
        cobj.Code = "12";
        cobj.Name = "Zeeshan";
        cobj.Amount = 7000;


        return View("DisplayCustomers",cobj);
    }

Now i have to take input from User, regarding cutomer iformation, using html page as mentioned in tutorial. so i tried adding a new webform under view folder, and and modified my controller as:

[HttpPost]
    public ViewResult DisplayCustomers()
    {
        Customer cobj = new Customer();

        cobj.Code = Request.Form["Id"].ToString();
        cobj.Name = Request.Form["Name"].ToString();
        cobj.Amount = Convert.ToDouble(Request.Form["Amount"].ToString());

        return View("DisplayCustomers",cobj);
    }

My Question is: How can i make my project stared, so that it takes input first, and then displays it, using above controller? Did i add the webform at right location? What would be the link to run it? i tried localhost:xyz/Default2/entryform etc. but failed. (in my entryform.aspx, i have mentioned form action="DisplayCustomer" )

1
  • how is your view look like? when you say modify your controller, is that mean in the controller you only have one HttpPost method?? or you also have one HttpGet as well? Commented Oct 4, 2013 at 13:18

2 Answers 2

1

It sounds like what you're missing is an action to just display the form. In otherwords, you just need an action to display a form. That form's POST action should reference your controller's DisplayCustomers action.

So in your controller code:

public class CustomerController : Controller 
{
    [HttpGet]
    public ViewResult New()
    {
       return View("NewCustomer");  //Our view that contains the new customer form.
    }

    // Add your code for displaying customers below
}

And in your view, you have code like this

@using(Html.BeginForm("DisplayCustomers", "Customer")) {
    <!-- Add your form controls here -->
}

Notice that I'm using the version of the BeginForm helper that specifies the action method and controller to call. This will write the form tag to post back to your DisplayCustomers action. Here is the equivalent HTML:

<form method="POST" action="/Customer/DisplayCustomers">

You would then access your form using the URL http://test.server/Customer/New.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. I want to know 2 things. 1: from your answer, i assume that I have created a controller named, Customer that is for our entry form view, named, NewCustomers. And in our entry form, we have specified that it will call another controller, named DisplayCustomers, which will display customer view. Did i get it right? 2: Let's suppose, i'm not using this html helper thing, then i'll have to specify the <form action="Displaycustomerpage"> .. right?
@Zeeshan Your first assumption is half right. Yes, we created a new controller called Customer, but both New and DisplayCustomers are a part of that one controller. As for your second question, if you don't use the helper the method will be POST and the action will be test.server/Customer/DisplayCustomers. I'll update my answer with that detail.
Fine. I've started getting the chemistry :) you mean, in the same controller, we have 2 methods, for 2 views. right?
You got it! Controllers are meant to house the behavior of the object, entity, or area of concern that they're named after.
Thanks ALOT :) and why do i use [HttpPost] for DisplayCustomers() ?
|
0

This may not be the best example in the world...but this will at least get you rolling..

url would be:localhost:1234/Home/Customer

the controller

public ActionResult Customer()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Customer(FormCollection frm)
    {

        var name = frm["name"].ToString();
        var address = frm["address"].ToString();

        ViewBag.Name = name;
        ViewBag.Address = address;

        return View();
    }

The view

<div>
    @using (Html.BeginForm())
    {
        <input type="text" name="name" id="name" />
        <input type="text" name="address" id="address"/>

        <input type="submit" name="submit" value="submit" />

         <input type="text" name="namedisplay" value='@ViewBag.Name'/>
        <input type="text" name="addressdisplay"  value='@ViewBag.Address'/>

    }
</div>

1 Comment

Thanks. can you please explain, where should i keep the view, and what its name, and where my app has specified to call the view first? Sorry, I'm newbie.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.