简体   繁体   中英

Partial view inside parent view not sending data to controller - asp.net MVC

I have a partial view name test/index

following is the code -

@model MyFirstMVC.Models.testModels
@using (Html.BeginForm())
{ 
     @Html.TextBox("test1")
     <input type="submit" value="click" />
     @ViewBag.a
}

And the controller of this partial view is following -

    [HttpPost]
    public ActionResult Index(MyFirstMVC.Models.testModels m)
    {
        ViewBag.a = Request["test1"].ToString() + " from controler";
        return View(m);
    }

In my parent view I am embedding my partial view like below -

 @Html.Partial("../test/index",new MyFirstMVC.Models.testModels())

When I run the application, the partial view do appear in the parent view. But when I enter value inside partial view and submit then nothing is happening (the value is not forwarding to the controller of partial view - I have checked that the controller action method related to the partial view is not invoking either by setting break-point).

Any help will be appreciated. Why my partial view is not sending value to its controller?

You need to specify the controller and action names in Html.BeginForm() if your not posting back to the same method that generated the view

@model MyFirstMVC.Models.testModels
@using (Html.BeginForm("Index", "Test"))
{ 
    @Html.TextBox("test1")
    <input type="submit" value="click" />
    @ViewBag.a
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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