MVC模板编辑和发布

时间:2014-06-30 23:23:54

标签: asp.net-mvc asp.net-mvc-4 mvc-editor-templates editortemplates

我是ASP.NET MVC 4的初学者,我遇到了问题。基本上我有这个控制器:

public ViewResult Login()
{
    return View(new LoginViewModel());
}

[HttpPost]
public ActionResult Login(LoginViewModel model)
{
    if (ModelState.IsValid)
    {
        if (authProvider.Authenticate(model.LoginUserName, model.LoginPassword))
        {
            return Redirect(Url.Action("Index", "Home"));
        }
        TempData["message"] = "Nome utente e/o password errati!!!";
        return View();
    }
    return View();
}

实现了一个简单的登录视图。我还创建了一个ViewModel:

public class LoginViewModel
{
    [Required(ErrorMessage = "Il nome utente è obbligatorio")]
    [UIHint("TextBoxLogin")]
    public string LoginUserName { get; set; }

    [Required]
    public string LoginPassword { get; set; }
}

最后我创建了EditorTemplate:

@model string

<input name="@ViewData.TemplateInfo.HtmlFieldPrefix" id="@ViewData.TemplateInfo.HtmlFieldPrefix"data-validation="required" data-validation-error-msg="@ViewData["HelpMessage"]" value="@Model" />

到目前为止一切顺利。问题在于视图。如果我把它放在视图中:

@using(Html.BeginForm()) { 
    @Html.ValidationSummary(true) 
    @Html.EditorForModel() 
    <p><input type="submit" value="Log in" /></p> 
} 

它的工作方式就像一个魅力(但是它会把很多不希望的html放到页面中),事实上,当我点击提交按钮时,它会转到控制器的POST actionResult。如果我这样说:

@using (Html.BeginForm("Login","Account",FormMethod.Post))
{
    <p class="username">
        <label for="UserName">Nome utente</label>
        @Html.EditorFor(m => m.LoginUserName, new { HelpMessage = "Il nome utente è obbligatorio!!!" });
    </p>
    <p class="password">
        <label for="Password">Password</label>
        @Html.EditorFor(m => m.LoginPassword)
    </p>
    <p>
        <input type="submit" value="Log in" />
    </p>
}

它不会发布在post actionresult上,但总是在Get one上。我想把这种类型的代码(最后一个)放在哪里我可以设置完全的html但我希望它继续POST Actionresult,有人可以帮助我理解为什么吗?

-----------------更新---------------- 这是生成的HTML:

<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<title>Title</title>
<meta name="robots" content="noindex,nofollow" />
<link href="/static/css/login.css" rel="stylesheet" type="text/css" />
<link href="/static/css/jquery_ui.css" rel="stylesheet" type="text/css" />
<!--[if lt IE 9]><link href="/static/css/lt_ie9.css" rel="stylesheet" type="text/css" /><![endif]-->
<script src="/static/js/jquery_1_10_2.js"></script>
<script src="/static/js/jquery_ui.js"></script>
<script src="/static/js/jquery_ui_function.js"></script>
</head>
<body>
<form>

<div id="top">
    <div class="wrapped">
        <div id="logo">TITLE</div>
    </div>
</div>
    <div id="content" class="user_student">
        <div class="wrapped">
            <div class="login_intro">
                <h2>TEST</h2>
            </div>
            <div class="login_input">
                <p id="error_messages"></p>
                <h2>THIS ONE MAKES GET REQUEST</h2>
<form action="/Account/Login" method="post">                        <p class="username"><label for="UserName">Nome utente</label>
                            <!--<input id="UserName" name="UserName" type="text"/>-->
                            <input name="LoginUserName" id="LoginUserName"data-validation="required" data-validation-error-msg="Il nome utente &#232; obbligatorio!!!" />;
                        </p>
                        <p class="password"><label for="LoginPassword">Password</label>
                            <input class="text-box single-line" data-val="true" data-val-required="Il campo LoginPassword è obbligatorio." id="LoginPassword" name="LoginPassword" type="text" value="" />
                        </p>
                        <p><input type="submit" value="Log in" /></p>
</form>                <p class="hidden">old</p>
            </div>
            <div class="login_footer">
                <p>FOOTER</p>
            </div>
        </div>  
    </div>
    <h2>THIS ONE MAKE POST REQUEST</h2>
<form action="/Account/Login?ReturnUrl=%2f" method="post"><div class="editor-label"><label for="LoginUserName">LoginUserName</label></div>
<div class="editor-field"><input name="LoginUserName" id="LoginUserName"data-validation="required" data-validation-error-msg="" /> <span class="field-validation-valid" data-valmsg-for="LoginUserName" data-valmsg-replace="true"></span></div>
<div class="editor-label"><label for="LoginPassword">LoginPassword</label></div>
<div class="editor-field"><input class="text-box single-line" data-val="true" data-val-required="Il campo LoginPassword è obbligatorio." id="LoginPassword" name="LoginPassword" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="LoginPassword" data-valmsg-replace="true"></span></div>
    <p><input type="submit" value="Log in" /></p> 
</form><script src="/static/form-validator/jquery.form-validator.min.js"></script>
<script src="/static/js/jquery_form_validator_function.js"></script>

</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

最后我弄清楚问题是什么......当然,这是世界上最愚蠢的事情。 在渲染的代码中,在主体之后有一个开放,在其内部是MVC从剃刀视图中放置的形式。 感谢大家对@DavidG的特别帮助