如何使用Ajax.BeginForm()帮助程序传递对象

时间:2014-06-21 15:20:45

标签: ajax asp.net-mvc partial-views

我正在尝试使用Ajax.BeginForm将消息发回服务器,我需要为表单命名,即使用参数htmlAttributes。为此,我使用以下签名:

@using (Ajax.BeginForm("SendEmail", "Contacts", null, new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "sendMessageArea" }, new { Name = "sendEmail" }))

此代码无法正常工作,它会更新整个页面,控制器期望将对象作为参数:

公共ActionResult SendEmail(联系人联系人)

以下是观点:

<div class="row">
<div class="large-12 columns" >
    @using (Ajax.BeginForm("SendEmail", "Contacts", null, new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "sendMessageArea" }, new { Name = "sendEmail" }))
    {
        <fieldset class="form-group">
            <legend>Leave a message</legend>
            <div class="panel">
                <div class="row">                    
                    <div class="large-offset-2 large-8 columns">                        
                            <div class="row">
                                <div class="large-2 columns">
                                    <div class="form-group">
                                        @Html.LabelFor(model => model.Name, new { @class = "control-label" })
                                    </div>
                                </div>
                                <div class="large-8 columns">                                    
                                    @Html.TextBox("Name", Model.Name, new { placeholder = "Your name..." })
                                    @Html.ValidationMessageFor(model => model.Name)
                                </div>
                                <div class="large-2 columns">
                                </div>
                            </div>
                            <div class="row">
                                <div class="large-2 columns">
                                    <div class="form-group">                                        
                                        @Html.LabelFor(model => model.EMail, new { @class = "control-label" })
                                    </div>
                                </div>
                                <div class="large-8 columns">
                                    <div class="form-group">
                                        @Html.TextBox("EMail", Model.EMail, new { placeholder = "Your email..." })
                                        @Html.ValidationMessageFor(model => model.EMail)
                                    </div>
                                </div>
                                <div class="large-2 columns">
                                </div>
                            </div>
                            <div class="row">
                                <div class="large-2 columns">
                                    <div class="form-group">
                                        @Html.LabelFor(model => model.Message, new { @class = "control-label" })
                                    </div>
                                </div>
                                <div class="large-8 columns">                                    
                                        @Html.TextArea("Message", Model.Message, new { placeholder = "Your message here...", @class = "comment-control", id = "message" })
                                        @Html.ValidationMessageFor(model => model.Message)                                    
                                </div>
                                <div class="large-2 columns">
                                </div>
                            </div>
                            <div class="row">
                                <div class="large-offset-8 large-3 columns">
                                    <input type='submit' style='position:absolute;top:0;left:-9999px;width:1px;height:1px' />
                                    <a class='submit-button' href="javascript:sendEmail.submit()">
                                        <span class='submit-button-inner'>Submit</span>
                                    </a>
                                </div>
                            </div>                        
                    </div>
                    <div class="large-2 columns">
                    </div>
                </div>
            </div>
        </fieldset>
    }
</div>

和部分视图:

<span class="success label">@Resources.Contacts.Index.SuccessSendMessage</span>

我认为问题应该与routeValues参数有关,因为如果我使用一个没有htmlAttributes和routeValues的简单示例,它就可以正常工作。 有什么猜测吗?

0 个答案:

没有答案