asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了asp.net – MVC 4 – 在局部视图中使用不同的模型大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
请忍受我的noobness,我是MVC模式的超新人。

我想做什么

我在我的网站上为注册用户建立个人资料信息页面。该页面将列出用户的数据,例如出生日期,电话号码,订阅状态等。您可以获得想法。我还希望有一个表单让用户在同一页面上更改他们的密码,电子邮件地址和个人信息。

我的问题

用户的数据通过传递的模型变量来自我的控制器:

public ActionResult Profil()
        {
            var model = db.Users.First(e => e.UserName == WebSecurity.CurrentUserName);
            return View(model);
        }

在我看来,输出如下:

<label>Phone number: </label>
            @if (Model.Phonenumber != null)
                    {
                        @model.Phonenumber
                    }
                    else
                    {
                        <span class="red">You haven't set up your phone number yet. </span>
                    }

用户可以更改其信息的形式将使用另一个模型ProfileModel。所以基本我需要使用两个模型在我的看法,一个用于输出信息,一个用于发布数据。我以为使用部分视图我可以实现这一点,但我得到这个错误:

这是我对局部视图的呼吁如下所示:

@using (Html.beginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSumMary()

        @Html.Partial("_ModifyProfileInfo")
    }

这是部分视图:

@model Applicense.Models.ProfileModel
<ul>
    <li>
        @Html.LabelFor(m => m.Email)
        @Html.EditorFor(m => m.Email)
    </li>
    <li>
        @Html.LabelFor(m => m.ConfirmEmail)
        @Html.EditorFor(m => m.ConfirmEmail)
    </li>
    <input type="submit" value="update e-mail" />
</ul>

最后这是我的ProfileModel:

public class ProfileModel
    {
        [required]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "New e-mail address")]
        public @R_616_10495@ng Email { get; set; }

        [DataType(DataType.EmailAddress)]
        [Display(Name = "Confirm new e-mail address")]
        [Compare("Email",Errormessage = "The e-mail and it's confirmation field do not match.")]
        public @R_616_10495@ng ConfirmEmail { get; set; }
    }

我缺少什么?这样做的正确方法是什么?

编辑:
我重写了我的代码,反映了Nikola Mitev的答案,但现在我还有另一个问题。这里是我得到的错误:

这只发生在我发布更改的电子邮件地址值时。这是我的ViewModel(ProfileModel.cs):

public class ProfileModel
    {
        public User UserObject { get; set; }

        [required]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "Új e-mail cím")]
        public @R_616_10495@ng Email { get; set; }

        [DataType(DataType.EmailAddress)]
        [Display(Name = "Új e-mail cím megerősítése")]
        [Compare("Email",Errormessage = "A két e-mail cím nem egyezik.")]
        public @R_616_10495@ng ConfirmEmail { get; set; }

        [DataType(DataType.EmailAddress)]
        [Display(Name= "E-mail cím")]
        public @R_616_10495@ng ReferEmail { get; set; }
    }

控制器:

public ActionResult Profil()
        {
            var User = db.Users.First(e => e.UserName == WebSecurity.CurrentUserName);

            var ProfileViewModel = new ProfileModel
            {
                UserObject = User
            };

            return View(ProfileViewModel);
        }

最后这里是我的user.cs模型类:

[Table("UserProfile")]
    public class User
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedoption.Identity)]
        public int UserId { get; set; }
        [column("UserName")]
        public @R_616_10495@ng UserName { get; set; }
        [column("Email")]
        [required]
        public @R_616_10495@ng Email { get; set; }
        [column("FirstName")]
        public @R_616_10495@ng FirstName { get; set; }
        [column("LastName")]
        public @R_616_10495@ng LastName { get; set; }
        [column("Phonenumber")]
        public @R_616_10495@ng Phonenumber { get; set; }
... You get the idea of the rest...

我在想,它正在发生,因为该模型试图将数据放在每个必需的列到数据库。

编辑2:
Profil操作的httppost方法:

[httpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult Profil(ProfileModel model)
        {
            if (ModelState.IsValid)
            {
//insert into database
                return Content("everything's good");
            }
            else
            {
//outputs form errors
                return View(model);
            }
        }

解决方法

处理这种情况的最好方法是使用并将viewModel传递给您的Profile控制器,viewModel是要传递给您的视图的多个对象的包装类。
public class ProfileUserviewModel
{
   public ProfileModel ProfileModelObject {get; set;}
   public UserModel  UserModelObject {get; set;}
}

您的控制器应如下所示:

public ActionResult Profil()
{            
    var profileModel = db.Users.First(e => e.UserName == WebSecurity.CurrentUserName);
    var userModel = //fetch from db.

    var pmViewModel = new ProfileUserviewModel  
                          {
                              ProfileModelObject = profileModel,UserModelObject = userModel
                          };

   return View(pmViewModel);
}

最后你的看法:

@model Applicense.Models.ProfileUserviewModel

<label>Phone number: </label>

@if (Model.ProfileModelObject.Phonenumber != null)
{
   @model.Phonenumber
}
else
{
    <span class="red">You haven't set up your phone number yet. </span>
}

大佬总结

以上是大佬教程为你收集整理的asp.net – MVC 4 – 在局部视图中使用不同的模型全部内容,希望文章能够帮你解决asp.net – MVC 4 – 在局部视图中使用不同的模型所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。