How can I pass data to the action ValueFromView from parent view and from child (partial) view? Also I want to use ActionLink i.e. want to redirect (not simple postbak)
@model MVCApp.Models.Test1@using MVCApp.Models;
@{
ViewBag.Title = "Index";
}@using (Html.BeginForm())
{
Test2 t2 = newTest2();
<h2>
Index</h2>
Html.RenderPartial("PartialTest", t2 );
@Html.ActionLink("Call to ValueFromView() from Parent", "ValueFromView", new { value = t2.Value })
}
Child (Partial) View
@model MVCApp.Models.Test2@using (Html.BeginForm())
{
@Html.EditorFor(model => model.Value)
@Html.ActionLink("Call to ValueFromView() from Child", "ValueFromView", new { value = Model.Value })
}
@Html.ActionLink("Call to ValueFromView() from Parent", "ValueFromView", new { value = t2.Value })
muhammad_sami
@Html.ActionLink("Call to ValueFromView() from Child", "ValueFromView", new { value = Model.Value })
Route value get mapped with name, so you need to either modify action parameter name as 'value' or change the route value parameter name to 'x' in ActionLink.
Thanks CPrakash82, thats right. I changed public ActionResult ValueFromView(int x) WITH public ActionResult ValueFromView(int value)
BUT I always get "value=0" no matter whatever I type in Editbox either I click @Html.ActionLink("Call to ValueFromView() from Child", "ValueFromView", new { value = Model.Value }) OR @Html.ActionLink("Call to ValueFromView() from Child", "ValueFromView", new { value = Model.Value }).
I want to pass value input by user in the EditBox to the Action ValueFromView(int value) when user click any of the ActionLink's
I know I am not very clear in asking the question, sorry about that.
@Html.Partial("TestP", new ViewDataDictionary{{"id","Ashok"} });
will pass data to the partial view called "TestP". I try to explain my question like that:
1) There is an Editor control in my partial view defined as under (see my orignal question for full definition)
@Html.EditorFor(model => model.Value)
2) I am want to pass value of this Editor control to the the action "ValueFromView" as under:
@Html.ActionLink("Call to ValueFromView() from Child", "ValueFromView", new { value = Model.Value })
3) problem is that when user clicks this ActionLink no value is passed to ValueFromView. (You can see the definition of
@Html.ActionLink("Call to ValueFromView() from Child", "ValueFromView", new { value = Model.Value })
I see, while form load your Model.Value is 0 and your action link is getting generated with value 0. If you need value from TextBox or Editor template then you need to use Javascript to get it. Also, if you want you can have a submit button to pass the value
in post.
You are right Model.Value is 0 and ActionLink is getting generated with value 0.
Certainly with help of submit button I will be posting back to [HttpPost] action only i.e. can't choose ANYOTHER desired action if I postback. Should we call it a flaw in MVC because I have to switch to JavaScript to do this?
no, its not a MVC flaw. please read and understand w3c from processing. You should not be using MVC with such poor undering of the underlying web technologies:
muhammad_sam...
Member
13 Points
28 Posts
Data Transfer from Parent View and Partial View to Action Method
Nov 28, 2012 04:39 PM|LINK
How can I pass data to the action ValueFromView from parent view and from child (partial) view? Also I want to use ActionLink i.e. want to redirect (not simple postbak)
Controller:
CPrakash82
All-Star
18314 Points
2851 Posts
Re: Data Transfer from Parent View and Partial View to Action Method
Nov 28, 2012 11:34 PM|LINK
Route value get mapped with name, so you need to either modify action parameter name as 'value' or change the route value parameter name to 'x' in ActionLink.
public ActionResult ValueFromView(int value)
muhammad_sam...
Member
13 Points
28 Posts
Re: Data Transfer from Parent View and Partial View to Action Method
Nov 29, 2012 11:16 AM|LINK
Thanks CPrakash82, thats right. I changed public ActionResult ValueFromView(int x) WITH public ActionResult ValueFromView(int value)
BUT I always get "value=0" no matter whatever I type in Editbox either I click @Html.ActionLink("Call to ValueFromView() from Child", "ValueFromView", new { value = Model.Value }) OR @Html.ActionLink("Call to ValueFromView() from Child", "ValueFromView", new { value = Model.Value }).
I want to pass value input by user in the EditBox to the Action ValueFromView(int value) when user click any of the ActionLink's
ashoksudani
Participant
1043 Points
213 Posts
Re: Data Transfer from Parent View and Partial View to Action Method
Nov 29, 2012 12:18 PM|LINK
You can use Like this
@Html.Partial("TestP", new ViewDataDictionary{{"id","Ashok"} });And
In Partial View You get This Value in ViewBag like
@ViewBag.id
muhammad_sam...
Member
13 Points
28 Posts
Re: Data Transfer from Parent View and Partial View to Action Method
Nov 29, 2012 01:11 PM|LINK
Hi ahok,
I know I am not very clear in asking the question, sorry about that.
@Html.Partial("TestP", new ViewDataDictionary{{"id","Ashok"} });
will pass data to the partial view called "TestP". I try to explain my question like that:
1) There is an Editor control in my partial view defined as under (see my orignal question for full definition)
CPrakash82
All-Star
18314 Points
2851 Posts
Re: Data Transfer from Parent View and Partial View to Action Method
Nov 29, 2012 02:15 PM|LINK
I see, while form load your Model.Value is 0 and your action link is getting generated with value 0. If you need value from TextBox or Editor template then you need to use Javascript to get it. Also, if you want you can have a submit button to pass the value in post.
muhammad_sam...
Member
13 Points
28 Posts
Re: Data Transfer from Parent View and Partial View to Action Method
Nov 29, 2012 02:44 PM|LINK
You are right Model.Value is 0 and ActionLink is getting generated with value 0.
Certainly with help of submit button I will be posting back to [HttpPost] action only i.e. can't choose ANYOTHER desired action if I postback. Should we call it a flaw in MVC because I have to switch to JavaScript to do this?
bruce (sqlwo...
All-Star
36904 Points
5452 Posts
Re: Data Transfer from Parent View and Partial View to Action Method
Nov 29, 2012 03:01 PM|LINK
no, its not a MVC flaw. please read and understand w3c from processing. You should not be using MVC with such poor undering of the underlying web technologies:
http://www.w3.org/TR/html401/interact/forms.html