从视图获取数据以在 asp.net MVC 中创建控制器

发布于 2024-10-17 22:51:05 字数 1924 浏览 3 评论 0原文

从视图获取数据以在 asp.net MVC 中创建控制器 我知道这很简单,但我刚刚学习 ASP.net MVC。

我有一个创建控制器和一个创建视图(使用生成器) 我可以将数据硬编码到控制器中,并且确实会被保存,但我想知道如何将用户放在表单上的数据返回到控制器中。 我的控制器是这样的。

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);
    } 

我的观点看起来像这样

  <% 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>

......很多属性,然后

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

<% } %>

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

我使用 ASP.net MVC 2(如果重要的话)。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再可℃爱ぅ一点好了 2024-10-24 22:51:05

您通常在控制器上执行两项操作:一项用于呈现表单,一项用于处理发布的表单值。通常它看起来像这样:

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);
    }
}

然后您的视图看起来如您所显示的那样,它包含一个表单和输入字段,允许用户填充模型的每个属性。当它提交表单时,将调用第二个操作,默认模型绑定器将自动使用用户在表单中输入的值填充操作参数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文