I am new to razor and I need help to pass the action name from controller to view using ViewBag. Following is my code:
public ActionResult Index(int id = 0)
{
ViewBag.MasterId = id;
ViewBag.ActionName = "ApplicationProcess";
return View();
}
From the above action method I am passing action name of that is being used in view that is render in result of this function.
<body>
<h2>Index</h2>
<div style="width: 1100px;">
<div style="float: left; width: 400px;">
#{Html.RenderAction("MasterRecordProcess", "Process");}
</div>
<div style="float: right">
#{Html.RenderAction("MasterRecordBasicInfo", "Process", new { id = ViewBag.MasterId });}
</div>
<div style="float:right;margin-top:50px;" class="task-div">
#{Html.RenderAction(#ViewBag.ActionName, "Process", null);}
</div>
</div>
</body>
Now in above view I want to use the ViewBag.ActionName passed from controller in Html.RenderAction but this is giving me error message.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper<OAC.Areas.Personal.Models.MasterModel>' has no applicable method named 'RenderAction' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
Kindly help how can I render the action name passed from controller in viewBag object. Thank you.
As #Stephen mentioned you must first cast the view bag content:
#{
var actionName= (string)ViewBag.ActionName;
}
Then you can use it:
#{Html.RenderAction(actionName, "Process", null);}
The error itself says that "consider casting the dynamic arguments or calling the extension method without the extension method syntax."
C# does not support calling an extension method (Html.RenderAction()) when any of the arguments is of a dynamic type. You have to either call the extension method statically or cast the argument to a non-dynamic type.
In this case you can the ViewBag to a String and use
Related
Can anyone tell how to write you own method and call it view or simply write "Hello world" in the following manner? This is what I tried to write on my
Index.cshtml:
<h1>
Hello,
<span>
#{ HttpContext.Current.Response.Write("World!"); }
</span>
</h1>
but it is not working? Thanks for any help :)
Response.Write is use for write string content to the responce,
If you want to bind your test to the view than you can create a model for the view and initialize from controller and pass it to the view see below example,
Here my model is type of string, you can create your complax model if thare are more than one property
Controller:
public ActionResult Index()
{
return View("index","World!");
}
View:
#model string
<h1>
Hello,
<span>
#Model
</span>
</h1>
In my ASP.NET MVC (5.2) project, I have a page called register.cshtml. It doesn't include any forms or anything, just plain divs.
Inside one of the divs, I'm rendering a partial:
#Html.Partial("~/Views/Users/_x.cshtml").
Inside _x.cshtml I have a form:
#using (Html.BeginForm("/users/x"))
{
...
}
When I go to my register page, I expect my form to be rendered as:
<form action="/users/x" method="post"> ... </form>
But instead, I'm getting this:
<form action="/users/register?Length=23" method="post" novalidate="novalidate"> ... </form>
What is length=23, why is there a novalidate attribute added, and why is it posting to an incorrect path?
Why is my form not rendering properly?
If your wanting to post to a method named x in usersController, then it needs to be
#using (Html.BeginForm("x", "users"))
{
....
}
Note that your currently using the overload that accepts object routeValues and because its a string, the method generated a route value for Length because that's the only property of string (the /users/register is because that the method that generated the main view)
From your code
Html.BeginForm("/users/x")
i understand that users your controller and x is a method. So you can do in this way-
#using (Html.BeginForm("x", "users", FormMethod.Post, new { id = "YourFormID"}))
{
}
#using (Html.BeginForm("action", "controller",new { QueryString = 1}, FormMethod.Post, null))
{
}
Note : its due to passing wrong parameter in beginform constructor .
and in ur VIEW
#Html.Partial("~/Views/Shared/_x.cshtml")
all, I could use your help again. I'm currently attempting to use an HTML helper to create a from that will pass my model into the Operations controller's method, TaskEdit, seen below:
[HttpPost]
public ActionResult TaskEdit(TaskViewModel viewModel, bool? embedded)
{
// code
}
In the view, I am using the following Razor code to attempt to produce the form:
#using (Html.BeginForm("TaskEdit", "Operations", new { embedded = true, viewModel = Model }, FormMethod.Post, new { #class = "form-horizontal" }))
{
// form code
}
This didn't actually give me my instance of the model - it only passed the class back as if it were a static class. So I tried the following instead:
#using (Html.BeginForm("TaskEdit", "Operations", new { embedded = true, id = Model.TaskId }, FormMethod.Post, new { #class = "form-horizontal" }))
{
// form code
}
And the following form was produced (which confused me):
<form action="/<sitename>/Operations/TaskEdit/0?embedded=True" class="form-horizontal" method="post"> <!-- Form code --> </form>
Not only was I assuming that the form action would be more along the lines of "/<sitename>/Operations/TaskEdit?id=0&embedded=True", but when I try to submit the form, I get a server error about "No parameterless constructor defined for this object." Help?
A transcription from the comments:
The reason the URL results in /TaskEdit/0 is because of the way your routing is setup. You'll notice the route also defines an id which causes it to format it different than expected.
The best solution here is to use a strongly typed view:
1) Put your model on top of the view (#model TaskViewModel)
2) Remove the model parameter from your form
3) Use the built-in extensions to create form fields (Html.EditorFor(x => x.SomeField)) or use the field names if you're doing it manually: <input type="text" name="SomeField />
4) The model parameter in your controller's actionresult will now contain the form data
My partial view, called _myTestView.cshtml
<div style="border:1px solid red;">
TEST
</div>
I call it like that
<div>
#Html.RenderPartial("_myTestView");
</div>
I get the following run time error
Compiler Error Message: CS1502: The best overloaded method match for
'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)'
has some invalid arguments
What am I doing wrong?
You're not inside a code block, so you don't use .RenderPartial
Just use .Partial and get rid of the ;
<div>
#Html.Partial("_myTestView")
</div>
Put it inside a code block
#{Html.RenderPartial("_myTestView");}
related What is the difference (if any) between Html.Partial(view, model) and Html.RenderPartial(view,model) in MVC2?
I'm trying to use Html.RenderPartial in acsx file
and I'm getting an error:
Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper'
has no applicable method named 'RenderPartial' but appears to have an
extension method by that name. Extension methods cannot be dynamically
dispatched. Consider casting the dynamic arguments or calling the
extension method without the extension method syntax
<a href="/projects/<%=project.Id %>">
<% Html.Label("fdf"); %>
<% Html.RenderPartial("ProjectName", Model.Id); %></a></li>
<%} %>
However I've import neccessary namespaces, so it won't be error at
<% Html.Label("fdf"); %>
Are there any methods to use Html.RenderPartial in ascx file?
The compiler cannot choose the correct method because your Model is dynamic. Change the call to:
<% Html.RenderPartial("ProjectName", (int)(Model.Id)); %>
Or any other datatype Id is.
In case anyone else made the same mistake I did:
#Model MyViewModel
This will treat your model as dynamic
#model MyViewModel
This is a correctly strongly typed view. Note the lack of capitalisation!
Note, that this is Razor, unlike the original question.
The only way i found to pass eg. an IEnumerable was to create a local variable and pass in this one.
For Example
#{
IEnumerable<Demo.Models.Friend> friends = Model.Friends;
Html.RenderPartial("_FriendsList", friends);
}
Html.RenderPartial("_FriendsList", (IEnumerable<Demo.Models.Friends>)(Model.Friends)); did not work!