编辑记录后没有返回POST事件

时间:2011-03-30 12:47:10

标签: asp.net-mvc-3

我正在尝试编辑记录。 Controller代码是:

        public ActionResult EditFixture(int id)
    {
        RLSBCWebSiteDb _context = new RLSBCWebSiteDb();
        Fixture fixture = _context.Fixtures.Find( id );

        ViewData["Gender"] = new SelectList( Fixture.GetGenderList(""), "Value", "Text", fixture.Gender );
        ViewData["MatchTypeList"] = new SelectList( Fixture.GetMatchTypeList( "" ), "Value", "Text", fixture.MatchType );
        ViewData["TeamNameList"] = new SelectList( Fixture.GetTeamNameList( "" ), "Value", "Text", fixture.TeamName );
        ViewData["CommentsList"] = new SelectList( Fixture.GetCommentsList( "" ), "Value", "Text", fixture.Comments );

        return View( fixture );
    }

    [HttpPost]
    public ActionResult EditFixture( Fixture fixture )
    {
        ViewData["Gender"] = new SelectList( Fixture.GetGenderList( "" ), "Value", "Text", fixture.Gender );
        ViewData["MatchTypeList"] = new SelectList( Fixture.GetMatchTypeList( "" ), "Value", "Text", fixture.MatchType );
        ViewData["TeamNameList"] = new SelectList( Fixture.GetTeamNameList( "" ), "Value", "Text", fixture.TeamName );
        ViewData["CommentsList"] = new SelectList( Fixture.GetCommentsList( "" ), "Value", "Text", fixture.Comments );

        if (ModelState.IsValid)
        {
            rlsbcWebSite.Fixtures.Add( fixture );
            rlsbcWebSite.SaveChanges();

            return RedirectToAction( "DisplayFixtures", new { id = fixture.MatchDate.Year.ToString() } );
        }

        return View( fixture );
    }

视图代码如下:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<RLSBCWebSite.Domain.Entities.Fixture>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
UpdateFixture
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h1>Update the Fixture</h1>

<% using (Html.BeginForm()) { %>
    <%: Html.ValidationSummary(true) %>
    <fieldset>
        <legend>Fixture</legend>
        <%: Html.HiddenFor(model => model.FixtureID) %>
        <%: Html.Partial("CreateOrEditFixture") %>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
<% } %>

<div class="grid_2">
    <%: Html.ActionLink("Back to List", "Index") %>
    <br /><br />
</div>

</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="scriptContent" runat="server"> </asp:Content>

部分代码如下:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<RLSBCWebSite.Domain.Entities.Fixture>" %>

        <%: Html.LabelFor(model => model.Gender) %>
        <%: Html.DropDownListFor( model => model.Gender, (ViewData["GenderList"] as SelectList) ) %>
        <%: Html.ValidationMessageFor(model => model.Gender) %>

        <%: Html.LabelFor(model => model.MatchType) %>
        <%: Html.DropDownListFor( model => model.MatchType, (ViewData["MatchTypeList"] as SelectList) )%>
        <%: Html.ValidationMessageFor(model => model.MatchType) %>

        <%: Html.LabelFor(model => model.TeamName) %>
        <%: Html.DropDownListFor( model => model.TeamName, (ViewData["TeamNameList"] as SelectList) )%>
        <%: Html.ValidationMessageFor(model => model.TeamName) %>

        <%: Html.LabelFor(model => model.MatchDate) %>
        <%: Html.TextBox( "MatchDate", Model.MatchDate.ToShortDateString() )%>
        <%: Html.ValidationMessageFor(model => model.MatchDate) %>

        <%: Html.LabelFor(model => model.Opponents) %>
        <%: Html.EditorFor(model => model.Opponents) %>
        <%: Html.ValidationMessageFor(model => model.Opponents) %>

        <%: Html.LabelFor(model => model.Venue) %>
        <%: Html.EditorFor(model => model.Venue) %>
        <%: Html.ValidationMessageFor(model => model.Venue) %>

        <%: Html.LabelFor(model => model.StartTime) %>
        <%: Html.EditorFor(model => model.StartTime) %>
        <%: Html.ValidationMessageFor(model => model.StartTime) %>

        <%: Html.LabelFor(model => model.ScoreFor) %>
        <%: Html.EditorFor(model => model.ScoreFor) %>
        <%: Html.ValidationMessageFor(model => model.ScoreFor) %>

        <%: Html.LabelFor(model => model.ScoreAgainst) %>
        <%: Html.EditorFor(model => model.ScoreAgainst) %>
        <%: Html.ValidationMessageFor(model => model.ScoreAgainst) %>

        <%: Html.LabelFor(model => model.Comments) %>
        <%: Html.DropDownListFor( model => model.Comments, (ViewData["CommentsList"] as SelectList) )%>
        <%: Html.ValidationMessageFor(model => model.Comments) %>

上面已删除<div>

编辑记录的调用显示数据OK,包括下拉列表的正确选择。我做了一些更改,然后选择Save按钮。调用正确的POST事件,但Fixture fixture参数为null。 ModelState无效并返回错误消息

The parameter conversion from type 'System.String' to type 'RLSBCWebSite.Domain.Entities.Fixture' failed because no type converter can convert between these types.

为什么Fixture类型的灯具没有正确传回。

2 个答案:

答案 0 :(得分:1)

我建议您使用编辑器模板:

<% using (Html.BeginForm()) { %>
    <%: Html.ValidationSummary(true) %>
    <fieldset>
        <legend>Fixture</legend>
        <%: Html.HiddenFor(model => model.FixtureID) %>
        <%: Html.EditorForModel() %>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
<% } %>

~/Views/Shared/EditorTemplates/Fixture.ascx内部:

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<RLSBCWebSite.Domain.Entities.Fixture>" 
%>

<%: Html.LabelFor(model => model.Gender) %>
<%: Html.DropDownListFor( model => model.Gender, (ViewData["GenderList"] as SelectList) ) %>
<%: Html.ValidationMessageFor(model => model.Gender) %>

<%: Html.LabelFor(model => model.MatchType) %>
<%: Html.DropDownListFor( model => model.MatchType, (ViewData["MatchTypeList"] as SelectList) )%>
<%: Html.ValidationMessageFor(model => model.MatchType) %>

<%: Html.LabelFor(model => model.TeamName) %>
<%: Html.DropDownListFor( model => model.TeamName, (ViewData["TeamNameList"] as SelectList) )%>
<%: Html.ValidationMessageFor(model => model.TeamName) %>

<%: Html.LabelFor(model => model.MatchDate) %>
<%: Html.TextBox( "MatchDate", Model.MatchDate.ToShortDateString() )%>
<%: Html.ValidationMessageFor(model => model.MatchDate) %>

<%: Html.LabelFor(model => model.Opponents) %>
<%: Html.EditorFor(model => model.Opponents) %>
<%: Html.ValidationMessageFor(model => model.Opponents) %>

<%: Html.LabelFor(model => model.Venue) %>
<%: Html.EditorFor(model => model.Venue) %>
<%: Html.ValidationMessageFor(model => model.Venue) %>

<%: Html.LabelFor(model => model.StartTime) %>
<%: Html.EditorFor(model => model.StartTime) %>
<%: Html.ValidationMessageFor(model => model.StartTime) %>

<%: Html.LabelFor(model => model.ScoreFor) %>
<%: Html.EditorFor(model => model.ScoreFor) %>
<%: Html.ValidationMessageFor(model => model.ScoreFor) %>

<%: Html.LabelFor(model => model.ScoreAgainst) %>
<%: Html.EditorFor(model => model.ScoreAgainst) %>
<%: Html.ValidationMessageFor(model => model.ScoreAgainst) %>

<%: Html.LabelFor(model => model.Comments) %>
<%: Html.DropDownListFor( model => model.Comments, (ViewData["CommentsList"] as SelectList) )%>
<%: Html.ValidationMessageFor(model => model.Comments) %>

对此代码的进一步改进是摆脱所有ViewData并使用强类型视图模型。

答案 1 :(得分:0)

我也遇到过这个问题,我也使用了编辑器模板。 尝试删除&lt;%:Html.HiddenFor(model =&gt; model.FixtureID)%&gt; 隐藏字段传递字符串值,这是我收到错误的地方。希望这能解决你的问题。