简体   繁体   English

MVC 2表单值未过帐

[英]MVC 2 Form Values not posting

Hey all I am attempting to learn MVC 2 and ASP etc through the MVC Music Store. 嗨,我正在尝试通过MVC音乐商店学习MVC 2和ASP等。 At the same time I am attempting to conform what it is doing to a solution I am developing at work. 同时,我试图使其工作与我正在开发的解决方案相一致。 The overall structure is an IT Help Desk ticket system and I am working on the very broad admin functions of creating, editing, and deleting tickets. 总体结构是一个IT Help Desk票证系统,我正在研究非常广泛的管理功能,包括创建,编辑和删除票证。 I have followed the tutorial very closely but have hit a brick wall, when attempting to use values that should be getting posted to controller methods, and theyre not getting there. 我已经非常仔细地阅读了本教程,但是在尝试使用应该发布到控制器方法的值时却遇到了麻烦,但并没有到达该方法。

For the create section my create.aspx looks like 对于create部分,我的create.aspx看起来像

<h2>Create</h2>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm()) {%>
    <%: Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Create New Request</legend>
        <%: Html.EditorFor(model => model.request,
               new {Softwares = Model.SoftwareName, Systems = Model.SystemIDNo}) %>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<% } %>

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

the partial view its calling is workRequest.ascx 部分视图的调用是workRequest.ascx

<p> 
    <%= Html.LabelFor(model => model.Medium)%> 
    <%= Html.TextBoxFor(model => model.Medium)%>
    <%= Html.ValidationMessageFor(model => model.Medium)%> 
</p> 
<p> 
    <%= Html.LabelFor(model => model.Summary)%> 
    <%= Html.TextBoxFor(model => model.Summary)%> 
    <%= Html.ValidationMessageFor(model => model.Summary)%> 
</p> 
<p> 
    <%= Html.LabelFor(model => model.Details)%> 
    <%= Html.TextAreaFor(model => model.Details)%> 
    <%= Html.ValidationMessageFor(model => model.Details)%> 
</p>
<p> 
    <%= Html.LabelFor(model => model.WorkHalted)%> 
    <%= Html.TextBoxFor(model => model.WorkHalted)%>  
    <%= Html.ValidationMessageFor(model => model.WorkHalted)%> 
</p>
<p> 
    <%= Html.LabelFor(model => model.Frequency)%> 
    <%= Html.TextBoxFor(model => model.Frequency)%>
    <%= Html.ValidationMessageFor(model => model.Frequency)%> 
</p>
<p> 
    <%= Html.LabelFor(model => model.StartDate)%> 
    <%= Html.TextBoxFor(model => model.StartDate, String.Format("{0:g}", Model.StartDate))%> 
    <%= Html.ValidationMessageFor(model => model.StartDate)%> 
</p>
<p>
    <%= Html.LabelFor(model => model.SoftwareID) %>
    <%= Html.DropDownList("SoftwareID", new SelectList(ViewData["Softwares"] as IEnumerable, Model.SoftwareID)) %>
</p>
<p>
    <%= Html.LabelFor(model => model.SystemID) %>
    <%= Html.DropDownList("SystemID", new SelectList(ViewData["Systems"] as IEnumerable, Model.SystemID)) %>
</p>

and the post create controller looks like 和后创建控制器看起来像

[HttpPost]
    public ActionResult Create(WorkRequest newRequest)
    {
        try
        {
            storeDB.AddToWorkRequests(newRequest);
            storeDB.SaveChanges();

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }

    }

I put a break point in the try and checked the values coming into newRequest and everything in newRequest is null, like nothing is getting passed. 我在尝试中放置了一个断点,并检查了传入newRequest的值,并且newRequest中的所有内容均为null,就像什么都没有传递一样。

A similar situation occurs on the edit side of things as well, nothing is getting sent from the partial view to the controller at all. 事物的编辑方面也发生类似的情况,根本没有任何东西从局部视图发送到控制器。

Anyways I am sure its something fairly simple, I am new to MVC, ASP, C#, pretty much all of it. 无论如何,我确信它相当简单,我几乎是MVC,ASP,C#的新手。 I dont normally ask other people for much, but I have been looking at this problem for quite some time and could use some fresh eyes on this one. 我通常不会向别人要求太多,但是我已经在这个问题上研究了很长时间了,并且可以在这个问题上用新的眼光。

Thanks in advance! 提前致谢!

Try using the same view model type as parameter that you used to render the view and to which it is strongly typed: 尝试使用与用于渲染视图的参数相同的视图模型类型,并将其强类型化:

[HttpPost]
public ActionResult Create(ViewModelUsedToStronglyTypeTheView model)
{
    //model.request should be properly bound here
    try
    {
        storeDB.AddToWorkRequests(model.request);
        storeDB.SaveChanges();
        return RedirectToAction("Index");
    }
    catch
    {
        return View(model);
    }
}

Or using a prefix: 或使用前缀:

[HttpPost]
public ActionResult Create([Bind(Prefix = "request")] WorkRequest newRequest)
{
    //model.request should be properly bound here
    try
    {
        storeDB.AddToWorkRequests(newRequest);
        storeDB.SaveChanges();
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

I believe you just need to change your action signature to look like this.. 我相信您只需要更改操作签名即可,如下所示。

public ActionResult Create(WorkRequest request)

The reason for this is the name of the values being posted. 原因是要发布的值的名称。 They look something like RequestViewModel.Request.Summary so the name of the request object is actually Request . 它们看起来像RequestViewModel.Request.Summary所以请求对象的名称实际上是Request There is no Request property on a WorkRequest object so nothing binds. WorkRequest对象上没有Request属性,因此没有任何绑定。

You can bind to the object your view is tied to, RequestViewModel , as Darin Dimitrov suggested. 您可以按照Darin Dimitrov的建议将视图绑定到的对象RequestViewModel绑定。

Alternatively, you can just bind to the request as you intended, you just need to ensure MVC knows how to bind the values. 或者,您可以按预期的方式绑定到请求,只需要确保MVC知道如何绑定值即可。 It only knows these things via naming convention so if you are binding a sub-object you need to ensure your param for the action is named the same as the property of it's parent object. 它仅通过命名约定知道这些内容,因此,如果要绑定子对象,则需要确保操作的参数与其父对象的属性相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM