道歉,如果这看起来很平凡,但我是MVC的初学者,这个看似简单的任务让我头疼不已。
我无法理解为什么我的代码无效。我正在从我的Index.cshtml提交一个表单,并且回发告诉我资源/索引(我发布的页面)不存在。
我可以帮忙解决一下我做错了吗?
我的观点(Index.cshtml):
@model MyProject.Connection
@{
ViewBag.Title = "Index";
}
<div id="container" class="container centered primary">
@using (Html.BeginForm(FormMethod.Post))
{
<img src="@Url.Content("/Content/images/default.png")" alt="CMGR Web" />
<div class="divider"></div>
<fieldset id="fs_server" class="borderless!T">
<legend>Server details</legend>
<table>
<tr>
<td>
@Html.LabelFor(c => c.serverHost)
</td>
<td>
@Html.TextBoxFor(c => c.serverHost)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(c => c.instanceName)
</td>
<td>
@Html.TextBoxFor(c => c.instanceName)
</td>
</tr>
</table>
</fieldset>
<fieldset id="fs_user" class="borderless!T">
<legend>Your credentials</legend>
<table>
<tr>
<td>
@Html.LabelFor(c => c.username)
</td>
<td>
@Html.TextBoxFor(c => c.username)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(c => c.password)
</td>
<td>
@Html.PasswordFor(c => c.password)
</td>
</tr>
</table>
</fieldset>
<div class="divider"></div>
<table>
<tr>
<td>
@Html.CheckBoxFor(c => c.remember)
@Html.Label("Remember me?")
</td>
<td>
<input type="submit"/>
</td>
</tr>
</table>
}
</div>
我的模型(Connection.cs):
namespace MyProject.Models
{
public class Connection
{
[Display(Name="Server Host")]
public string serverHost { get; set; }
[Display(Name="Instance Name")]
public string instanceName { get; set; }
[Display(Name = "Username")]
public string username { get; set; }
[Display(Name = "Password")]
public string password { get; set; }
[Display(Name = "Remoting Password")]
public string remotingPassword { get; set; }
[Display(Name = "Persistent")]
public bool remember { get; set; }
}
}
我的控制器(IndexController.cs)
namespace MyProject.Controllers
{
public class IndexController : Controller
{
//
// GET: /Login/
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
private ActionResult Index(Connection channel)
{
return View();
}
}
}
答案 0 :(得分:2)
您的Post ActionResult设置为Private,因此无法访问。将其更改为公开
private ActionResult Index(Connection channel)
{
return View();
}
public ActionResult Index(Connection channel)
{
return View();
}