开发者

Asp.Net MVC View with Multiple strongly typed Partial View with Submits per partial view

I am trying to create a View in ASP.NET MVC in which different parts of a view model are modified separately to capture intent.

For example, the modification on users can be done in one of the following ways at a time:

  1. Modify Name,
  2. Modify Email,
  3. Modify Password,
  4. Modify Status
public class UserEditViewModel
{
    public string DepartmentDetails { get; set; }
    [HiddenInput(DisplayValue = false)]
    public Guid UserId { get; set; }

    public class UserNameEditModel
    {
        [Required(ErrorMessage = "Name Required")]
        [DisplayName("Name")]
        [StringLength(50, ErrorMessage = "Name must be less than or equal to 50 characters")]
        public string Name { get; set; }
    }

    public class UserPasswordEditViewModel
    {
        [Required(ErrorMessage = "Password cannot be Empty")]
        [DisplayName("Password")]
        public string Password { get; set; }

        [Required(ErrorMessage = "Confirm Password is Required")]
        [DisplayName("Confirm Password")]
        public string ConfirmPassword { get; set; }
    }

    public class UserStatusEditViewModel
    {
        [Required(ErrorMessage = "Status  Required")]
        [DisplayName("Stat开发者_运维知识库us")]

        public bool  Status { get; set; }

        [Required(ErrorMessage = "Comment  Required")]
        [DisplayName("Comment")]
        public string Comment { get; set; }
    }
}

What I want to do its create strongly typed partial views based on outerclass+ one of inner classes. Each partial view will have its own form submission which will have action pointed out to a separate controller action (edit password, editname, editstatus...)

I tried t0 create view based on outerclass+ one of inner classes, which is something I got in the automated view builder wizard but it was unable to resolve DepartmentDetails as well as UserID of outerclass.

  1. Is what I am wanting to do possible in ASP.NET MVC 2 / 3 Beta?
  2. I will use Ajax later on for enhancement but I would like to do as much form post without it.
  3. If it is possible to have separate forms on partial view work independently then I guess one solution might be to have User Id and Details on each of the EditviewModel and and have view use one single ViewModel.


I'd suggest that you have 1 model to generate the view and different models for actions (change pass etc).

In my opinion it would be easier. You could assemble all the properties in one class for displaying a view with different forms, but separate them in your post actions.


I'm not certain I completely understand the question... however if all you're trying to do is submit the same form data to different actions based on which button the user clicks... I suggest you look here: How do you handle multiple submit buttons in ASP.NET MVC Framework?

Based on one of the answers in the thread I linked above, I ended up creating an attribute that allows MVC to redirect form submission to the correct action.

I don't really want to post my code, as it's still in the proof of concept stage... but it works and it feels fairly clean.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜