简体   繁体   English

MVC3剃刀局部视图不会发送回控制器

[英]MVC3 Razor Partial View does not send back to controller

I have a partial view that has a create button on it however it never reaches the controller once pressed. 我有一个局部视图,上面有一个创建按钮,但是一旦按下它就永远不会到达控制器。 When pressed it goes to index ie /Rebate from /Rebate/Edit/1 当按下它进入索引,即/ Rebate / Edit / 1中的/ Rebate

@model RMS.Models.RebateLine



@using (Html.BeginForm("Create","RebateLine",FormMethod.Post )) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>RebateLine</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.RebateID)
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

Edit: Added controller logic 编辑:添加了控制器逻辑

    public class RebateLineController : BaseController
{
   public ActionResult Create()
    {
        return View();
    } 

    //
    // POST: /RebateLine/Create

    [HttpPost]
    public ActionResult Create(RebateLine rebateline)
    {
        if (ModelState.IsValid)
        {
            UnitOfWork.RebateLineRepository.Insert(rebateline);
            UnitOfWork.Save();

            return RedirectToAction("Index");  
        }

        return View(rebateline);
    }
...
  }

Additional Edit: /Rebate is what is shown after I click create and below is the call to the partial which is being called. 附加编辑:/ Rebate是单击“创建”后显示的内容,下面是对被调用的部分的调用。

  @{ Html.RenderPartial("_RebateLines",Model.RebateLines.FirstOrDefault() ); }

Final Edit: I have redesigned this so partials are not need to do anything but display but if someone comes up with an answer I will attempt to go back at a later date and fix it up. 最终编辑:我已经对此进行了重新设计,因此分部除了显示外不需要做任何其他事情,但是如果有人想出答案,我会尝试稍后再修复。

It seems your Controller is named RebateController . 看来您的控制器名为RebateController If that is the case then change: 如果是这种情况,请更改:

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

to

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

EDIT 编辑

Create a DebugController and add a an action called: Test1. 创建一个DebugController并添加一个名为Test1的操作。 Create the corresponding View for it. 为其创建相应的视图。 Do not use any Layout page. 不要使用任何布局页面。

Just add this markup to the body section: 只需将此标记添加到主体部分:

<div>
@{Html.RenderPartial("_RebateLines",new Model.RebateLines());}
</div>

Run the project in debug mode and visit the /debug/test1 url. 在调试模式下运行项目,并访问/ debug / test1网址。 Place a breakpoint in your RebatteLines::Create action. 在您的RebatteLines :: Create动作中放置一个断点。 Hit the submit button. 点击提交按钮。 Breakpoint must be hit. 必须命中断点。 If it is hit that means that there is something in the page where you are calling Html.RenderPartial that is preventing the form from submitting to the expected url, probably some javascript. 如果命中,则意味着页面中正在调用Html.RenderPartial的某些内容阻止了表单提交到期望的URL,可能是一些javascript。 Might even be in your layout. 甚至可能在您的布局中。 Confirm if test I mention above runs as expected. 确认我上面提到的测试是否按预期运行。

You are using a controller named "RebateLineController" and a ActionResult "Create". 您正在使用名为“ RebateLineController”的控制器和名为“ Create”的ActionResult。 But you have mentioned the url as "/Rebate/Edit/1". 但是您提到的URL为“ / Rebate / Edit / 1”。 Is the url correct? 网址正确吗?

And from your code: 从您的代码:

if (ModelState.IsValid)
    {
        UnitOfWork.RebateLineRepository.Insert(rebateline);
        UnitOfWork.Save();

        return RedirectToAction("Index");  
    }

it is clear that if the modelstate is valid, the page will be redirected to "Index" as you have given: 显然,如果modelstate有效,则页面将按照您指定的那样重定向到“索引”:

return RedirectToAction("Index");

Am I correct? 我对么? This is what I can assume from your code. 这就是我可以从您的代码中得出的结论。

Regards.. Sunil 问候..苏尼尔

Since all your code looks good. 由于所有代码看起来都不错。 I would take a look Glimpse this might help you track down the issue. 我想看看瞥见这可以帮助你追踪问题。

Try using 尝试使用

@Html.RenderAction("Create", "RebateLines", new { id = Model.RebateLines.FirstOrDefault().RebateID })

And change your controller as below: 并如下更改控制器:

public class RebateLineController : BaseController
{
   public ActionResult Create(int id)
    {
      return View();
    } 

//
// POST: /RebateLine/Create

[HttpPost]
public ActionResult Create(int id,RebateLine rebateline)
{
    if (ModelState.IsValid)
    {
        UnitOfWork.RebateLineRepository.Insert(rebateline);
        UnitOfWork.Save();

        return RedirectToAction("Index");  
    }

    return View(rebateline);
}
...
}

Hope it helps.. 希望能帮助到你..

Mark as answer if useful. 如果有用,请标记为答案。

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

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