我有以下视图。
<div class="form-group">
@Html.LabelFor(m => m.LoginId, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.LoginId, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.LoginId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
@Html.ActionLink("Log in", "Login1", null, new { @class = "link" })
</div>
</div>
而这是javascript。
$(function () {
$('.link').click(function () {
var loginid = $("#LoginId").val();
var pass = $("#Password").val();
this.href = this.href + '?loginid=' + encodeURIComponent(loginid);
this.href = this.href + '?password=' + encodeURIComponent(pass); // Something is going wrong here!
});
});
这是控制器的Action方法。
public ActionResult Login1(string loginid,string password)
{
if (loginid == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
User user = db.Users.Find(loginid);
if (user == null)
{
return HttpNotFound();
}
return RedirectToAction("Index", "Products");
}
我输入了loginid=a和password=b.但问题是:它发送的是loginid=:a?password=b和password=null.如何才能正确发送参数?
将按钮设为提交按钮,点击后表单会自动发布,你不必写任何其他代码。
确保你的文本框名称(而不是id)与你的操作中的参数名称一致。
我认为,我不是亲,错误是在ajax函数中。'?'是造成这个问题的原因。当前是如下。
this.href = this.href + '?loginid=' + encodeURIComponent(loginid);
this.href = this.href + '?password=' + encodeURIComponent(pass);
但应该是:
this.href = this.href + '?loginid=' + encodeURIComponent(loginid);
this.href = this.href + '&password=' + encodeURIComponent(pass);