0

Okay so, i am totally new to MVC and I'm trying to wrap my head around a few of the concepts. I've created a small application...

This application has a view for creating a new Individual record. The view is bound to a model ViewPage... And I have a associated IndividualController which has a New method...

The New method of the IndividualController looks like this...

public ActionResult New()
    {
        var i = new Individual();

        this.Title = "Create new individual...";
        i.Id = Guid.NewGuid();

        this.ViewData.Model = new Individual();


        return View();
    }

Now, the above all seems to be working. When the view loads I am able to retrieve the data from the Individual object. The issue comes into play when I try and save the data back through the controller...

In my IndividualController I also have a Save method which accepts an incoming parameter of type Individual. The method looks like...

   public ActionResult Save(IndividualService.Individual Individual)
    {
        return RedirectToAction("New");
    }

Now, on my view I wanted to use a standard html link/href to be used as the "Save" button so I defined an ActionLink like so...

 <%=Html.ActionLink("Save", "Save") %>

Also, defined in my view I have created a single textbox to hold the first name as a test like so...

 <% using (Html.BeginForm()) { %>
     <%=Html.TextBox("FirstName", ViewData.Model.FirstName)%>
 <% } %>

So, if I put a break point in the Save method and click the "Save" link in my view the break point is hit within my controller. The issue is that the input parameter of the Save method is null; even if I type a value into the first name textbox...

Obviously I am doing something completely wrong. Can someone set me straight...

Thanks in advance...

4

3 回答 3

1

Your New controller method doesn't need to create an individual, you probably just want it to set the title and return the view, although you may need to do some authorization processing. Here's an example from one of my projects:

    [AcceptVerbs( HttpVerbs.Get )]
    [Authorization( Roles = "SuperUser, EditEvent, EditMasterEvent")]
    public ActionResult New()
    {
        ViewData["Title"] = "New Event";
        if (this.IsMasterEditAllowed())
        {
            ViewData["ShowNewMaster"] = "true";
        }

        return View();
    }

Your Save action should take the inputs from the form and create a new model instance and persist it. My example is a little more complex than what I'd like to post here so I'll try and simplify it. Note that I'm using a FormCollection rather than using model binding, but you should be able to get that to work, too.

    [AcceptVerbs( HttpVerbs.Post )]
    [Authorization( Roles = "SuperUser, EditEvent, EditMasterEvent")]
    public ActionResult Save( FormCollection form )
    {
         using (DataContext context = ...)
         {
              Event evt = new Event();
              if (!TryUpdateModel( evt, new [] { "EventName", "CategoryID", ... }))
              {
                  this.ModelState.AddModelError( "Could not update model..." );
                  return View("New");  // back to display errors...
              }
              context.InsertOnSubmit( evt );
              context.SubmitChanges();
              return RedirectToAction( "Show", "Event", new { id = evt.EventID } );
         }
    }
于 2009-01-16T15:13:11.873 回答
0

如果我没有在 New 方法中创建一个新的 Indvidual 对象,那么当我的视图尝试将文本框绑定到关联模型时,我会在我的视图中的下面一行得到一个 NullReferenceException ......

`<%=Html.TextBox("FirstName", ViewData.Model.FirstName)%>`

关于 Save 方法。据我了解,由于我的视图是强类型的,我不应该能够拥有像...这样的方法签名。

   `public ActionResult New(IndividualService.Individual ind)
    {
        return View();
    }`

我认为这是模型绑定背后的目的..?

于 2009-01-16T15:30:49.653 回答
0

我强烈建议您从您正在做的事情中退后一步,并在此处浏览一些教程/视频http://www.asp.net/learn/

If you have a strongly typed Viewit means that when the Controllerpicks that view to generate the output the view has better access to the Model.

但是,对于随后从客户端返回的内容(例如发布表单或以其他方式导航到的 URL)View负责。

ASP.NET-MVC 使用 URL 中的信息来确定Controller将请求传递给哪个。之后,控制器负责将请求中的各种其他元素解析为Model类的实例。

由于 ASP.NET-MVC 路由为控制器提供的重要帮助,传入请求和控制器之间的关系变得模糊不清。例如,可以定义一个路由来为控制器方法提供参数,这就是控制器所需要的,因此您看不到与 http 请求相关的方法中的任何代码。但是应该理解,contoller方法只是处理一个http请求。

我希望您可以从上面看到,在请求生命周期中,将模型中的类的实例传递给控制器​​上的公共方法还为时过早。如果需要实例化,由控制器决定哪些模型类。

于 2009-01-16T20:36:39.740 回答