I'm new to MVC and trying to pass data from a view to page and am having two problems:
The ID that is in the page url is not being passed to the controller
(customers/details/1)
I cannot get the variable to be written to the page. (i've been told
to try avoiding the use of viewbag and viewdata).
My controller looks like this:
public class CustomersController : Controller
{
public ActionResult Details(int? pageIndex)
{
var Name = "Nope";
if(pageIndex == 1)
{
Name = "John Smith";
};
return View(Name);
}
}
}
My view look like this:
#{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Details</h2>
<p>#Model.Name</p>
Related
Hey guys I've got this function:
if (Cookies.CheckIfCookiesExists())
{
int.TryParse(Cookies.getWorkerCookieId("u-Site_Admin"), out uid);
var worker = unitOfWork.Workers.Get(uid);
ViewBag.isSuperAdmin = worker.IsSuperAdmin;
}
I want to pass the isSuperAdmin property down to the layout, and I need a controller to do this check every time the user switches between tabs.
My home controller returns this view:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.Title = "";
ViewBag.isSuperAdmin = ViewBag.isSuperAdmin;
}
Inside the layout what I care about is the Aside page:
#{ Html.RenderPartial("~/Views/Shared/partials/_aside.cshtml"); }
How would I go about achieving this? Basically the problem is the ViewBag value is lost like I've stated.
In the controller, set the ViewBag property.
Render the partial view within the view. Note that I'm using Html.Partial() instead of Html.RenderPartial.
The ViewBag property will be passed to the partial view.
Controller:
public class HomeController : Controller
{
public ActionResult Test()
{
ViewBag.isSuperAdmin = true;
return View();
}
}
View:
#{
ViewBag.Title = "Test";
}
<h2>Test</h2>
#Html.Partial("~/Views/Shared/partials/_aside.cshtml")
Partial View:
<h3>ViewBag.isSuperAdmin = #ViewBag.isSuperAdmin</h3>
Result:
I have the same child action placed in several parents actions, let's say there are these two parent views :
#model Parent1Model
#{
ViewBag.Title = "Parent1";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#Html.Action("Child","MainControler")
The second parent :
#model Parent2Model
#{
ViewBag.Title = "Parent2";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#Html.Action("Child","MainControler")
All actions are placed in the same controller.
public ActionResult parent1() //GET
{
Parent1Model model = new Parent1Model()
return View(model );
}
public ActionResult parent2() //GET
{
Parent2Model model = new Parent2Model()
return View(model );
}
public ActionResult Child() //GET
{
ChildModel model = new ChildModel()
return View(model );
}
[HttpPost]
public ActionResult Child(ChildModel model) //POST
{
DoThingsWithResult(model)
// The next line should return the get of the parent action
return RedirectToParent() // This does not exist...
}
Now, how do i tell the child to call his RIGHT parent's GET? Being the child of many parents, how can he know which parent called it this time ?
I could not find anything, neither on google nor in the "Questions that may already have your answer".
You can add one more param to determine where it is coming from,
you can also make this param a string which will be the parent action name itself and pass that param to your RedirectToAction(param)
Ex
[HttpPost]
public ActionResult Child(string ParentName, ChildModel model) //POST
{
DoThingsWithResult(model)
// The next line should return the get of the parent action
return RedirectToAction(ParentName) // This does not exist...
}
in your parent action
public ActionResult parent2() //GET
{
Parent2Model model = new Parent2Model();
ViewBag.Parent = "parent2";
return View(model );
}
in your view
#model Parent2Model
#{
ViewBag.Title = "Parent2";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#Html.Action("Child","MainControler", new {ParentName= ViewBag.Parent})
Do the same for Parent Action 1
I am having issue on passing string parameter from controller to view in the controller I have:
namespace Map02.Controllers
{
public class AppController : Controller
{
public ActionResult Index(string name)
{
string str = name;
return View(str);
}
}
}
and in view I have:
#model string
#{
ViewBag.Title = "";
}
<h2>AppContent</h2>
<p>#str</p>
but I am getting this error:
To pass a string to the view as the Model, you can do:
public ActionResult Index()
{
string str = name;;
return View((object)str);
}
You must cast it to an object so that MVC doesn't try to load the string as the view name, but instead pass it as the model. You could also write:
return View("Index", str);
Then in your view, just type it as a string:
#model string
#{
ViewBag.Title = "";
}
<h2>AppContent</h2>
<p>#Model</p>
I am new to MVC, in my View I have :
#{
MyObjectType myNameObject=getMyNameObject();
}
<p>
Name : #myNameObject.firstName
</p>
Trying to implement desing when line "Name : #myNameObject.firstName" comes from database through Model or ViewBag and still pick up value of #myNameObject.firstName. Is that possible?
Thank you
Your Controller action method should get the data and pass it to the View. Using a ViewModel is usually the way to go, but ViewBag also works for one-off data. Something like this:
// View model.
public class MyViewModel
{
public string Name {get; set;}
}
// Controller method.
public ActionResult MyPage()
{
MyViewModel model = serviceLayer.GetMyData();
return View(model);
}
// MyPage View.
#model MyViewModel
<p>Name: #Model.Name</p>
your Model:
public class MyNameObject
{
public MyNameObject()
{
//getvalues from database
//this.FirstName = yourObjectFromDatabase.FirstName;
}
public string FirstName { get; set; }
}
in your views that you want to show the first name:
#using WebApplication1.Models; //put it at the top of the view
#{ Html.RenderPartial("FirstName", new MyNameObject()); }
your view:
#model WebApplication1.Models.MyNameObject
#{
ViewBag.Title = "FirstName";
}
<h2>Name : #Model.FirstName</h2>
I am developing an application. I have created a view and a controller. The view has a button, on the click of which I am supposed to do database operations. I have put the database operations in the model, I am creating the object of model in the controller. On clicking the button the action is handled by a method in the controller, and the object of the model is created to get the records from the database. I would like to know if there is any way to display this data in the view.Is the approach correct or the view is supposed to interact with model directly to get the data.
Following is the code in controller that gets invoked on the button click
public ActionResult getRecord()
{
DataModel f_DM = new DataModel();
DataTable f_DT = f_DM.getRecord();
return View();
}
DataModel is the model class with simply a method "getRecord".
Any help will be highly appreciated.
I would like to add that i am using vs2010 and mvc4
Regards
you should write the logic of retrieving data in your controller. Store all your data in view model and pass it to the view.
for eg.
Model
namespace Mvc4App.Models
{
public class Product
{
public string Name { get; set; }
}
public class ProductViewModel
{
public Product Product { get; set; }
public string SalesPerson { get; set; }
}
}
Controller
public class ProductController : Controller
{
public ActionResult Info()
{
ProductViewModel ProductViewModel = new ProductViewModel
{
Product = new Product { Name = "Toy" },
SalesPerson = "Homer Simpson"
};
return View(ProductViewModel);
}
}
View
#model Mvc4App.Models.ProductViewModel
#{ ViewBag.Title = "Info"; }
<h2>Product: #Model.Product.Name</h2>
<p>Sold by: #Model.SalesPerson</p>
This is the best known practice to pass data from controller to the view.
you may use other techniques also like,
1. ViewData
2. ViewBag
3. TempData
4. View Model Object
5. Strongly-typed View Model Object
Yes, it's possible, but actually now very logical way to to this.
Lets follow your way. You have some View were you have a button, that will trigger this action.
For ex:
public ActionResult Index()
{
return View();
}
Inside view you can have a Ajax link, that will trigget your getRecord method:
<div id="GetDataDiv"></div>
<div>
#Ajax.ActionLink("Get Record", "getRecord", "ControllerName", null, new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "GetDataDiv" })
</div>
In the getRecord method you should have:
public ActionResult getRecord()
{
DataModel f_DM = new DataModel();
DataTable f_DT = f_DM.getRecord();
return PartialView(f_DT);
}
And in View it should be:
#model DataTable
#Model.PropertyOne #Model.PropertyTwo
It should works for you.
Actually same exaple here: http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=151