Question

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" )

Was it helpful?

Solution

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.

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top