在Umbraco中上传会员图片时出现问题

时间:2019-03-18 12:33:34

标签: asp.net-mvc umbraco8

我正在使用Umbraco 8中的成员API,我正在尝试在注册时上传图像。我在后台注册会员并打开会员资料后,看到以下内容: enter image description here

这是我的Coontroller

    public class RegisterController : SurfaceController
{
    // GET: Register
    public ActionResult Register(Models.RegisterModel model)
    {
        if (!ModelState.IsValid)
            return CurrentUmbracoPage();

        var memberService = Services.MemberService;

        if (memberService.GetByEmail(model.Email) != null)
        {
            ModelState.AddModelError("", "A member with that email alredy exists");
            return CurrentUmbracoPage();
        }

        var member = memberService.CreateMemberWithIdentity(model.Email, model.Email, model.Name, "bMEMembers");

        member.SetValue("companyName", model.CompanyName );
        member.SetValue("avatar", model.Avatar);


        memberService.SavePassword(member, model.Password);
        Members.Login(model.Email, model.Password);

        memberService.Save(member);

        return Redirect("/");
    }
}

型号:

        public HttpPostedFileBase Avatar { get; set; }

查看:

    @Html.LabelFor(model => model.Avatar)
    <input type="file" name="Avatar" />
    @Html.ValidationMessageFor(model => model.Avatar)

    <input class="btn" type="submit" value="Create" />

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

出现通常是因为Avatar是一个对象(恰好是HttpPostedFileBase),而LabelFor将显示对象名称空间。

看看:https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.html.labelextensions.labelfor?view=aspnet-mvc-5.2

并发现该方法有重载:

public static System.Web.Mvc.MvcHtmlString LabelFor<TModel,TValue> (this System.Web.Mvc.HtmlHelper<TModel> html, System.Linq.Expressions.Expression<Func<TModel,TValue>> expression, string labelText, System.Collections.Generic.IDictionary<string,object> htmlAttributes);

您可以这样使用:

@Html.LabelFor(model => model.Avatar, "Avatar", null);


或更美的方式:

[DisplayName("Avatar")]
public HttpPostedFileBase Avatar { get; set; }

并保留您的CSHTML代码:

@Html.LabelFor(model => model.Avatar)