Pass ViewBag value to PartailView MVC - viewbag

My ViewBag value return empty when passing through PartialView.
CONTROLLER
[HttpGet]
public ActionResult Name()
{
ViewBag.Name = "Bob";
return PartialView();
}
View for above Name.cshtml
#{
<p>Name is: #ViewBag.Name </p>
}
Parent view Index.cshtml
#Html.Partial("Name")
When render it shows Name is: (blank) instead of Name is: Bob. I am still fairly new with MVC. Thank you for your help.

Please close this thread. I used #Html.Action("action","controller") and it solved my problem. Thank you.

Related

I want to send two parameters using href through anchor tag in ASP.NET MVC

This code is not hitting the action method. Can you suggest the correct code?
College is my controller and Details is my action name.
Try replacing this line of code:
With this line of code:
#Html.ActionLink("View Details","Details","Colleges",new { id = item.Id, CountryId = item.CountryId },null)
Hopefully, your action method header in Colleges controller looks something like this:
[HttpGet]
public IActionResult Details(int id, int countryId)
Goodluck!

Model binding with a child object

I have a class:
public class Application
{
....
public Deployment NewDeployment { get; set; }
....
}
I have an editor template for Deployment within the Application View folder.
The ApplicationViewModel has a SelectedApplication (of type Application), in my Index.cshtml where I use ApplicationViewModel as my Model, I have this call:
#using (Html.BeginForm("Create", "Deployment", new { #id = Model.SelectedId,
q = Model.Query }, FormMethod.Post, new { id = "form", role = "form" }))
{
#Html.EditorFor(m => m.SelectedApplication.NewDeployment)
}
Which then correctly renders out the control in my DisplayTemplates\Deployment.cshtml (though, it may just be pulling the display code and nothing in relation to the NewDeployment object's contents). All is well in the world until I go to submit. At this stage everything seems good. Controller looks like:
public class DeploymentController : Controller
{
[HttpPost]
public ActionResult Create(Deployment NewDeployment)
{
Deployment.CreateDeployment(NewDeployment);
return Redirect("/Application" + Request.Url.Query);
}
}
However, when it goes to DeploymentController -> Create, the object has nulls for values. If I move the NewDeployment object to ApplicationViewModel, it works fine with:
#Html.EditorFor(m => m.NewDeployment)
I looked at the output name/id which was basically SelectedApplication_NewDeployment, but unfortunately changing the Create signature to similar didn't improve the results. Is it possible to model bind to a child object and if so, how?
Your POST action should accept the same model your form is working with, i.e.:
[HttpPost]
public ActionResult Create(ApplicationViewModel model)
Then, you'll be able to get at the deployment the same way as you did in the view:
model.SelectedApplication.NewDeployment
It was technically an accident that using #Html.EditorFor(m => m.NewDeployment) worked. The only reason it did is because the action accepted a parameter named NewDeployment. If the parameter had been named anything else, like just deployment. It would have also failed.
Per Stephen Muecke's comment and with slight modifications, I was able to find how to correct it:
[HttpPost]
public ActionResult Create ([Bind(Prefix="SelectedApplication.NewDeployment")] Deployment deployment)
{
// do things
}

ActionLink doesn't work

I'm pretty sure that I'm doing something really stupid. Please have a look and let me know what I'm doing wrong.
Here is my ActionLink
#Html.ActionLink("Edit","Edit","UserProfile", new { id = Model.ApplicationUserId },null)
When I click this it throws the bad request exception and also I noticed the url is
https://localhost:44304/UserProfile/Edit/92b1347c-c9ec-4728-8dff-11bc9f935d0b
not
https://localhost:44304/UserProfile/Edit?userId=92b1347c-c9ec-4728-8dff-11bc9f935d0b
I have a HTTPGET Edit method in my controller and it takes UserId. When I pass the route values manually it works.Please help me.
Your help is much appreciated and someday, will pay it forward.
Thanks!
Cheers!
If the parameter you are expecting is userId, then use the #Html.ActionLink like this:
#Html.ActionLink("Edit","Edit","UserProfile", new { userId = Model.ApplicationUserId },null)
If you pass the parameter with name id, then the MVC will route like you mentioned:
https://localhost:44304/UserProfile/Edit/92b1347c-c9ec-4728-8dff-11bc9f935d0b
Which is great, but your method should be something expecting the parameter with the appropriate name:
// GET: /UserProfile/Edit/{id}
public ActionResult Edit(String id){
//your code
return View();
}
If you have some time, check out this ASP.NET MVC Routing Overview with a lot more details.
You need change parameter for your controller action Edit from userId to id - best variant.
public Edit(int id)
{
}
Or
#Html.ActionLink("Edit","Edit","UserProfile", new { userId = Model.ApplicationUserId },null)

How to Return view from a string in MVC3 in C#

public ActionResult Default()
{
ViewBag.Message = "Hello,World!";
var template = "<h1>#ViewBag.Message</h1>";
//How to make the content of the following "View" = "template"
return View();
}
Then build html result as:
<h1>Hello,World!</h1>
Plase help me,Thanks! I know RazorEngine can parse Razor file,but can't Render string to View.
use the content result instead, which returns the raw content. It does not use an associated view, but only returns content directly to the requestor.
return Content(template);

YSOD when passing complex type as razor model in NancyFX

I'm getting a YSOD when sending a model of type IEnumerable<string> to my Razor view in NancyFX. Everything works well if supply a string as the model, with the relevant #model statement in the view, so its working.
The error is
Unable to discover CLR Type for model by the name of System.Collections.Generic.IEnumerable. Ensure that the model passed to the view is assignable to the model declared in the view.
What have I missed?
View.cshtml
#model System.Collections.Generic.IEnumerable<System.String>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1></h1>
#foreach (var item in Model)
{
<h3>#item</h3>
}
</body>
</html>
The Module
public class MyModule: NancyModule
{
public MyModule()
{
Get["/"] = parameters => View["View", this.GetModel()];
}
private IEnumerable<string> GetModel()
{
return new[] { "one", "two" };
}
}
The problem seems to be the #model directive isn't supported in Nancy. Swapping the #model for an #inherits with the correct type fixes the issue:
#inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<My.ViewModels.WhateverViewModel>
In addtion to Greg B's answer, #model is nonetheless a reserved term in the RazorEngine for Nancy, even though this isn't clear from the Nancy Razor View Engine page.
So, you can't declare a variable with the name model and reference it with #model.Property for instance; the view engine will still try to bind it to the model, even if that doesn't actually work (Razor View Engine line 354) and you'll get the same error.