无法将源类型 Umbraco.Web.Models.RenderModel 绑定到模型类型 Repower.Cms.Umbraco.Models.Test

Cannot bind source type Umbraco.Web.Models.RenderModel to model type Repower.Cms.Umbraco.Models.Test

首先,我是 Umbraco 的新手,所以如果您看到一些基本错误,请不要评判我。

所以我目前正在创建一个登录表单,他进入数据库(检查用户名和密码)并读取他 returns 的值,让他 Cannot bind source type Umbraco.Web.Models.RenderModel to model type Repower.Cms.Umbraco.Models.Test.

这是我的 HTML:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Repower.Cms.Umbraco.Models.Test>

@{
    Layout = "Master.cshtml";
}

<style type="text/css">
    .btnStyle {
        border: thin solid #000000;
        line-height: normal;
        width: 80px;
    }
</style>
@using (Html.BeginForm("Test", "MembersProtectedPage", FormMethod.Post))
{
    <div class="fontStyle">
        <center>
            <table style="margin-top: 100px;margin-left:150px">
                <tr style="height:30px">
                    <td align="right">
                        @Html.LabelFor(m => m.User)
                    </td>
                    <td style="width:200px" align="right">
                        @Html.TextBoxFor(m => m.User)
                    </td>
                    <td style="width:250px;color:Red" align="left">
                        @Html.ValidationMessageFor(m => m.User)
                    </td>
                </tr>
                <tr style="height:30px">
                    <td align="right">
                        @Html.LabelFor(m => m.Password)
                    </td>
                    <td align="right">
                        @Html.PasswordFor(m => m.Password)
                    </td>
                    <td style="width:250px;color:Red" align="left">
                        @Html.ValidationMessageFor(m => m.Password)
                    </td>
                </tr>
                <tr style="height:30px">
                    <td colspan="2" align="center">
                        <input type="submit" value="Sign In" class="btnStyle" />
                    </td>
                </tr>
            </table>
        </center>
    </div>
}

这是我的模特:

    public class Test : RenderModel
{
    public Test() : this(new UmbracoHelper(UmbracoContext.Current).TypedContent(UmbracoContext.Current.PageId)) { }
    public Test(IPublishedContent content, CultureInfo culture) : base(content, culture) { }
    public Test(IPublishedContent content) : base(content) { }

    string connString = ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString;
    SqlConnection conn;
    SqlCommand sqlcomm;

    public string User { get; set; }
    public string Password { get; set; }

    public bool IsUserExist(string emailid, string password)
    {
        bool flag = false;
        conn = new SqlConnection(connString);
        conn.Open();

        sqlcomm = new SqlCommand();
        sqlcomm.Connection = conn;
        sqlcomm.CommandType = System.Data.CommandType.StoredProcedure;
        sqlcomm.CommandText = "dbo.uspLogin";
        sqlcomm.Parameters.AddWithValue("@pLoginName", User);
        sqlcomm.Parameters.AddWithValue("@pPassword", Password);

        SqlParameter retval = sqlcomm.Parameters.Add("@RESULT", SqlDbType.VarChar);
        retval.Direction = ParameterDirection.ReturnValue;
        sqlcomm.ExecuteNonQuery(); // MISSING
        string retunvalue = (string)sqlcomm.Parameters["@RESULT"].Value;

        switch (retunvalue)
        {
            case "0":
                flag = true;
                break;
            case "1":
                flag = false;
                break;
            case "2":
                flag = false;
                break;
            default:
                flag = false;
                break;
        }
        return flag;
    }
}

这是我的控制器:

public class TestController : Controller
{
    public ViewResult Login()
    {
        return View();
    }

[HttpPost, ValidateInput(false)]
public ActionResult Login(Test model)
{
    if (ModelState.IsValid)
    {
        if (model.IsUserExist(model.User, model.Password))
        {
            ViewBag.UserName = model.User;
            FormsAuthentication.RedirectFromLoginPage(model.User, false);
        }
        else
        {
            ModelState.AddModelError("", "Username or Password Incorrect.");
        }
    }
    return View(model);
   }
}

所以我继承了一个 RenderModel 因为之前我的错误是 "The model item passed into the dictionary is of type Repower.Cms.Umbraco.Models.Test', but this dictionary requires a model item of type 'Umbraco.Web.Models.RenderModel'." 所以我改变了它(在互联网上搜索了很多)现在我得到这个错误。

其余代码是否也正确?我访问数据库和所有内容的方式?我期待数据库中的 Return 值(不知道是否正确)

有人可以帮助我吗? 我今天需要完成它。

提前致谢

您的实施存在一些问题。

使用 Umbraco 会员服务

您正在重新发明轮子,构建一个新的 table 来保存会员信息(例如用户名和密码)。

Umbraco 内置了可以处理您站点成员的成员资格。您可以在 /umbraco/#/member 查看 Umbraco 中的 GUI。使用此 GUI,您可以手动创建和编辑成员。

您还可以使用此 MemberService.

以编程方式在此部分中创建最终编辑成员

例如注册会员:

var MemberService = ApplicationContext.Current.Services.MemberService

var member = MemberService.CreateMemberWithIdentity(newEmail, newEmail, newName, "Member");

MemberService.Save(member);
MemberService.SavePassword(member, newPassword);

FormsAuthentication.SetAuthCookie(newEmail, true);

登录:

var memberService = ApplicationContext.Current.Services.MemberService;

if (memberService.Exists(email))
{
    if (Membership.ValidateUser(email, password))
    {
        FormsAuthentication.SetAuthCookie(email, true);
    }
}

您可以阅读其他可用方法here

您正在混淆 MVC

您的 Test 模型不仅仅是一个模型,它还包含一些控制器,因为它也在处理数据库内容!

理想情况下,您的模型应该只包含已转发的数据,并且您的 TestController 应该处理使用该数据。

至于解决绑定问题

您目前正在将页面视图模型设置为 Repower.Cms.Umbraco.Models.Test,我认为应该保持原样。

阻止此模型继承自 RenderModel

相反,使用您的代码渲染部分内容。

您的页面浏览量:

@inherits Umbraco.Web.Mvc.UmbracoViewPage
@{
    Layout = "Master.cshtml";
}
@Html.Partial("Login", new Repower.Cms.Umbraco.Models.Test())

部分调用 Login.cshtml:

@model Repower.Cms.Umbraco.Models.Test
@using (Html.BeginUmbracoForm<TestController>("Login"))
{
    <div class="fontStyle">
        <center>
            <table style="margin-top: 100px;margin-left:150px">
                <tr style="height:30px">
                    <td align="right">
                        @Html.LabelFor(m => m.User)
                    </td>
                    <td style="width:200px" align="right">
                        @Html.TextBoxFor(m => m.User)
                    </td>
                    <td style="width:250px;color:Red" align="left">
                        @Html.ValidationMessageFor(m => m.User)
                    </td>
                </tr>
                <tr style="height:30px">
                    <td align="right">
                        @Html.LabelFor(m => m.Password)
                    </td>
                    <td align="right">
                        @Html.PasswordFor(m => m.Password)
                    </td>
                    <td style="width:250px;color:Red" align="left">
                        @Html.ValidationMessageFor(m => m.Password)
                    </td>
                </tr>
                <tr style="height:30px">
                    <td colspan="2" align="center">
                        <input type="submit" value="Sign In" class="btnStyle" />
                    </td>
                </tr>
            </table>
        </center>
    </div>
}

最后,更新您的控制器以继承自 SurfaceController

希望对您有所帮助

这不是完整的解决方案,但应该可以帮助您走上正轨。