0

我正在使用 Umbraco 8 中的成员 API,我正在尝试在注册时上传图像。在我注册会员并在后台打开会员资料后,我看到了这个: 在此处输入图像描述

这是我的控制器

    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" />

有人可以帮我解决这个问题吗?

4

1 回答 1

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)
于 2019-03-18T12:56:29.010 回答