Asp.Net MVC3 Razor提交调用方法

时间:2012-01-30 14:30:34

标签: asp.net asp.net-mvc

你好,我是新的我mvc,我有下一个观点

@model MeterProfileData.Core.Dto.CdapiDto

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/SiteMaster.cshtml";
}

<h2>Create</h2>


@using (Html.BeginForm()) {
    <fieldset>
        <legend>Add new CDAPI</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Url)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Url)
            @Html.ValidationMessageFor(model => model.Url)
        </div>
      <p>
        <input type="submit"  value="Save" />
      </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "CdapiList")
</div>

和下一个控制器

public class CdapiController : Controller
  {
    //
    // GET: /Cdapi/

    public ActionResult CdapiList()
    {
      var cdapiList = new CdapiService().GetListCdapiServers();
      return View(cdapiList);
    }

    [HttpGet]
    public ActionResult Create()
    {
      return View();
    }

    [HttpPost]
    public ActionResult Create(CdapiDto dto)
    {
      new CdapiService().CreateCdapi(dto);
      return RedirectToAction("CdapiList");
    }
  }

提交后总是调用第一个方法并刷新页面。我做错了什么?以及如何调用第二种方法并从页面

发送给它的参数

主要版面

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
</head>
<body>
    <div>
    </div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Test mvc</title>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <link rel="stylesheet" href="/Content/style.css" type="text/css" media="screen, projection">
    </link>
    <script src="@Url.Content("~/Content/javascript/jquery-1.7.1.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-jtemplates.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="wrapper">
        <div id="header">
            <h3>
                View Pages</h3>
            <ul>
                <li><a runat="server" href="/Register">Registers</a></li>
                <li><a runat="server" href="/MeterProfile">Meter Profiles</a></li>
                <li><a runat="server" href="/register/SupportedRegisters">Supported Registers </a></li>
                <li><a runat="server" href="/logers/SupportedLogers">Supported Loggers </a></li>
                <li><a runat="server" href="/Cdapi/CdapiList">CDAPI Servers </a></li>
            </u>
        </div>
        <div id="content">
            @RenderBody()
        </div>
        <div id="footer">
        </div>
    </div>
    <div>
    </div>
    </form>
</body>
</html>

2 个答案:

答案 0 :(得分:3)

在打开form标记后,您的主版块中还有另一个<body>代码。删除那个表格然后你应该好。

答案 1 :(得分:-1)

我不确定你想要实现什么,但你需要告诉它要发布到哪个动作:

@using (Html.BeginForm("Create", "Cdapi", FormMethod.Post))