Partial view with #HTML.Action(...) - c#

I have a strongly typed partial view that should show the name of an account that a user is logged into:
#model MyNamespace.Models.AccountNameViewModel
#if (Request.IsAuthenticated)
{
#Html.Action("AccountName", "AccountNameController", Model)
Logged in to #Model.AccountName
}
I have a controller:
public class AccountNameController : Controller
{
public ActionResult Index()
{
return View();
}
[ChildActionOnly]
public ActionResult AccountName(AccountNameViewModel model)
{
... Do somthing with the repository to populate the model
return PartialView(model);
}
}
What I want to do is add a shared partial view that displays the name of an account that a user is logged into. What I get is the following error:
The controller for path '/ParentViewPath/' was not found or does not implement IController.
Am I at least heading in the right direction?

You have to remove the controller part in your call
#Html.Action("AccountName", "AccountName", Model)
To render a partial view you can also call
#Html.Partial("AccountName", "AccountName", Model)

Related

MVC5 MasterName in View

I have simple Controller Action and knew how to pass model to rendered view but how to get masterName in this view?
public ActionResult Index()
{
var model =1;
return View("Index","some master",model);
}

Move from a controller to different views

I created a login page with the built in AccountModel and AccountController. Now I have another controller named TestController that sends data from a database to my TestView.
What I now wish to to is to send the data that I send from the TestController to be displayed within the AccountView.
Basically what I am trying to do is to display a feed on the loginpage at the accountview, problem is that the data comes from my TestController so I do not now how to pass this data.
So I have this:
Controller : AccountController -> Model: AccountModels -> View: Account -> login.cshtml
Controller : TestController -> Model: Compare -> View : Test -> index.cshtml
I wanna display the content from #2 within #1 view. Is this possible?
There are a few ways to do this. I highly recommend looking at the tutorial videos here
You could create an action in your TestController that returns a partial view.
public ActionResult Test()
{
return PartialView("index.cshtml");
}
In your login.cshtml view, you would display the partial view from TestController by calling #Html.Action("Test", "TestController")
Create an action with return type of ViewResult that will return the overloaded ViewResult which takes in string parameter. That string is the name of your view.
Or you can use RedirectAction to redirect to a different controller's action. I think this is what you are after.
If you want to redirect to Account page together with the data. Below would be my suggestion.
public ActionResult PostToAccount() {
var accountModel = new AccountModel();
return RedirectToAction("Account", "Account", accountModel);
}
If you want ajax based where both Account view and Test view are rendered in a single page. Try the ajax approach
Login.cshtml
<div id="feed"></div>
Index.cshtml
View
<script>
function postData() {
$.ajax({
url: '/Test/PostToAccount'
data: {
someData: 'feed'
},
success: function(data) {
$("#feed").data(data.result);
}
});
}
</script>
<button onclick="postData"></button>
Controller
public class TestController : Controller {
public ActionResult PostToAccount(string someData) {
var accountModel = new AccountModel();
accountModel.Feed = feed;
return View("~/Views/Test/Index.cshtml", accountModel);
}
}

How to load correctly a partial view in mvc?

In my mvc project i have a controller with the following actions:
public ActionResult Index()
{
return View(new List<Product>());
}
The corresponding index view render a partial view with the master grid:
#model System.Collections.Generic.List<TbbModels.Domain.Product>
#Html.Partial("_ProdutoMasterGrid", Model)
This partial view have a submit button and a grid. The client needs to put some data in the form and submit it to that action:
public ActionResult _ProdutoMasterGrid(string param)
{
return PartialView("_ProdutoMasterGrid",
repository.Compare(param).ToList());
}
But than i get the grid without the layout. How can i return a partial view with the layout?
You should explicitly return the correct view:
public ActionResult _ProdutoMasterGrid(string param)
{
return View("Index",
repository.Compare(param).ToList());
}
This ensures that when you do a post to this action, it returns the index view. I'm supposing here that repository.Compare return a List<Product> as well, since the type needs to match.

Annotation Validation not working

Please help me on the following code. The Model class is using System.ComponentModel.DataAnnotation:
namespace Proj.Models
{
public class Customer
{
[Required]
public string CustomerID{get;set;}
[Required]
public string CustomerName{get;set;}
}
}
I have created a controller using this model, the action method being:
public class Customer:Controller
{
public ActionResult Details()
{
return View();
}
}
The razor view is Details.cshtml, having following markup and code:
#model Proj.Models.Customer
<form method="post">
#Html.EditorForModel()
<button>Submit!!</button>
</form>
However, when I click submit, no validation errors are seen as expected.
You need to create a method which takes your model as input like this:
[HttpPost]
public ActionResult Index(Customer customer)
{
return View();
}
The [HttpPost] ensures that the method is only called on POST requests.
You need to create editor template for your model. By default no validation messages will be emitted. Inside your editor template, you will have to use #ValidationMessageFor for your Required fields.
Hope this helps.

Can two different controllers access a single view in mvc?

I have two different controllers and I want both of them to use a Common View.
Is that possible?
Thanks in advance!!!!
Yes.Mention the view full path in the View method.
public class UserController : Controller
{
public ActionResult ShowUser()
{
return View();
}
}
public class AccountController : Controller
{
public ActionResult ShowAccount()
{
return View("~/Views/User/ShowUser.cshtml");
}
}
If the name of your Views are same in both the controllers, You can keep the Common view under the Views/Shared directory and simply call the View method without any parameter. The View name should be same as the Action method name.
public class UserController : Controller
{
public ActionResult ShowUser()
{
return View();
}
}
public class AccountController : Controller
{
public ActionResult ShowUser()
{
return View();
}
}
Assuming you have a View called ShowUser.cshtml under Views/Shared folder.

Categories