开发者

Getting data from view to create controller in asp.net MVC

Getting data from view to create controller in asp.net MVC I know this is very simple but I am just learning ASP.net MVC.

I have a Create controller and a create view (used the generator) I can hard code data into the controller and that does get saved but I want to know how to get the data the user put on the form back into the controller. My controller is like this.

public ActionResult Create(Seller newSeller)
{
       if (ModelState.IsValid) 
       {          
           try 
           {                      
               newSeller.SellerID = 34324442;
               newSeller.State = "NA";
               newSeller.UserType = "Seller";
               newSeller.FirstName = "sdfasd";
               newSeller.LastName = "dasdfadsf";
               newSeller.Phone = "33333";
               newSeller.Email = "dfasdfasdf";

               // write to database
               listingsDB.Sellers.AddObject(newSeller);
               listingsDB.SaveChanges();                       

               return RedirectToAction("Details", newSeller.SellerID);         
            }         
            catch(Exception ex)
            {                     

            }     
        }      
        return View(newSeller);
    } 

My view looks like this

  <% using (Html.BeginForm()) {%>
        <%: Html.开发者_开发技巧ValidationSummary(true) %>

        <fieldset>
            <legend>Fields</legend>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.SellerID) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.SellerID) %>
                <%: Html.ValidationMessageFor(model => model.SellerID) %>
            </div>

... Lots of propterties and then

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

<% } %>

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

I am using ASP.net MVC 2 if it matters.


You usually have two actions on the controller: one for rendering the form and one for processing the posted form values. Typically it looks like this:

public class SellerController: Controller
{
    // used to render the form allowing to create a new seller
    public ActionResult Create()
    {
        var seller = new Seller();
        return View(seller);
    }

    // used to handle the submission of the form
    // the seller object passed as argument will be
    // automatically populated by the default model binder
    // from the POSTed form request parameters
    [HttpPost]    
    public ActionResult Create(Seller seller)
    {
        if (ModelState.IsValid)
        {
            listingsDB.Sellers.AddObject(seller);
            listingsDB.SaveChanges();                       
            return RedirectToAction("Details", new { id = seller.SellerID });
        }
        return View(seller);
    }
}

then your view looks as you have shown, it contains a form and input fields allowing the user to fill each property of the model. When it submits the form, the second action will be invoked and the default model binder will automatically fill the action parameter with the values entered by the user in the form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜