Partial View difficulty ASP.NET MVC - asp.net

In the partial view I have to access Session variable LoginInfo which holds a LoginInfo object. I tried the following:
#LoginInfo info = #(LoginInfo)Session["LoginInfo"]
But it gives me compiler errors. What am I doing wrong?

What am I doing wrong?\
Many things. The first thing you did wrong is the Razor syntax. It should be:
#{ LoginInfo info = (LoginInfo)Session["LoginInfo"]; }
But that's just the syntax. You have a far bigger problem. You have a design problem. You are violating the MVC pattern.
A view should not try to fetch any data. A view uses data that is passed to it from the controller action under the form of a view model.
So you define a view model:
public class MyViewModel
{
public LoginInfo LoginInfo { get; set; }
}
then you have a controller action:
public ActionResult Foo()
{
var model = new MyViewModel();
model.LoginInfo = (LoginInfo)Session["LoginInfo"];
return View(model);
}
then you have a strongly typed view in which you use the view model:
#model MyViewModel
<div>Hello #Model.LoginInfo.FirstName</div>
But in this case (given the name of the class LoginInfo) I suppose that you are trying to display some common widget on all views. That would be a great candidate for using the Html.Action helper.
So you define controller action that will fetch this information from some data source (Session in your case):
[ChildActionOnly]
public ActionResult LoginInfo()
{
var model = (LoginInfo)Session["LoginInfo"] ?? new LoginInfo();
return PartialView(model);
}
and then you define a corresponding partial:
#model LoginInfo
<div>Hello #Model.FirstName #Model.LastName</div>
and finally in your _layout you can include this widget somewhere:
#Html.Action("LoginInfo", "SomeController")

Related

Entering Value in Partial View and Posting it back to Main Controller in ASP.NET MVC 5 [duplicate]

I have a ViewModel that has a complex object as one of its members. The complex object has 4 properties (all strings). I'm trying to create a re-usable partial view where I can pass in the complex object and have it generate the html with html helpers for its properties. That's all working great. However, when I submit the form, the model binder isn't mapping the values back to the ViewModel's member so I don't get anything back on the server side. How can I read the values a user types into the html helpers for the complex object.
ViewModel
public class MyViewModel
{
public string SomeProperty { get; set; }
public MyComplexModel ComplexModel { get; set; }
}
MyComplexModel
public class MyComplexModel
{
public int id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
....
}
Controller
public class MyController : Controller
{
public ActionResult Index()
{
MyViewModel model = new MyViewModel();
model.ComplexModel = new MyComplexModel();
model.ComplexModel.id = 15;
return View(model);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
// model here never has my nested model populated in the partial view
return View(model);
}
}
View
#using(Html.BeginForm("Index", "MyController", FormMethod.Post))
{
....
#Html.Partial("MyPartialView", Model.ComplexModel)
}
Partial View
#model my.path.to.namespace.MyComplexModel
#Html.TextBoxFor(m => m.Name)
...
how can I bind this data on form submission so that the parent model contains the data entered on the web form from the partial view?
thanks
EDIT: I've figured out that I need to prepend "ComplexModel." to all of my control's names in the partial view (textboxes) so that it maps to the nested object, but I can't pass the ViewModel type to the partial view to get that extra layer because it needs to be generic to accept several ViewModel types. I could just rewrite the name attribute with javascript, but that seems overly ghetto to me. How else can I do this?
EDIT 2: I can statically set the name attribute with new { Name="ComplexModel.Name" } so I think I'm in business unless someone has a better method?
You can pass the prefix to the partial using
#Html.Partial("MyPartialView", Model.ComplexModel,
new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "ComplexModel" }})
which will perpend the prefix to you controls name attribute so that <input name="Name" ../> will become <input name="ComplexModel.Name" ../> and correctly bind to typeof MyViewModel on post back
Edit
To make it a little easier, you can encapsulate this in a html helper
public static MvcHtmlString PartialFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string partialViewName)
{
string name = ExpressionHelper.GetExpressionText(expression);
object model = ModelMetadata.FromLambdaExpression(expression, helper.ViewData).Model;
var viewData = new ViewDataDictionary(helper.ViewData)
{
TemplateInfo = new System.Web.Mvc.TemplateInfo
{
HtmlFieldPrefix = string.IsNullOrEmpty(helper.ViewData.TemplateInfo.HtmlFieldPrefix) ?
name : $"{helper.ViewData.TemplateInfo.HtmlFieldPrefix}.{name}"
}
};
return helper.Partial(partialViewName, model, viewData);
}
and use it as
#Html.PartialFor(m => m.ComplexModel, "MyPartialView")
If you use tag helpers, the partial tag helper accepts a for attribute, which does what you expect.
<partial name="MyPartialView" for="ComplexModel" />
Using the for attribute, rather than the typical model attribute, will cause all of the form fields within the partial to be named with the ComplexModel. prefix.
You can try passing the ViewModel to the partial.
#model my.path.to.namespace.MyViewModel
#Html.TextBoxFor(m => m.ComplexModel.Name)
Edit
You can create a base model and push the complex model in there and pass the based model to the partial.
public class MyViewModel :BaseModel
{
public string SomeProperty { get; set; }
}
public class MyViewModel2 :BaseModel
{
public string SomeProperty2 { get; set; }
}
public class BaseModel
{
public MyComplexModel ComplexModel { get; set; }
}
public class MyComplexModel
{
public int id { get; set; }
public string Name { get; set; }
...
}
Then your partial will be like below :
#model my.path.to.namespace.BaseModel
#Html.TextBoxFor(m => m.ComplexModel.Name)
If this is not an acceptable solution, you may have to think in terms of overriding the model binder. You can read about that here.
I came across the same situation and with the help of such informative posts changed my partial code to have prefix on generated in input elements generated by partial view
I have used Html.partial helper giving partialview name and object of ModelType and an instance of ViewDataDictionary object with Html Field Prefix to constructor of Html.partial.
This results in GET request of "xyz url" of "Main view" and rendering partial view inside it with input elements generated with prefix e.g. earlier Name="Title" now becomes Name="MySubType.Title" in respective HTML element and same for rest of the form input elements.
The problem occurred when POST request is made to "xyz url", expecting the Form which is filled in gets saved in to my database. But the MVC Modelbinder didn't bind my POSTed model data with form values filled in and also ModelState is also lost. The model in viewdata was also coming to null.
Finally I tried to update model data in Posted form using TryUppdateModel method which takes model instance and html prefix which was passed earlier to partial view,and can see now model is bound with values and model state is also present.
Please let me know if this approach is fine or bit diversified!

MVC 4 Action Result er

I am new in Asp.net MVC 4
I write function using MVC controller
public ActionResultRole_name(string role)
{
return role;
}
My question is how to display return value on cshtml razor view?
You could return a ViewResult:
public ActionResult Role_name(string role)
{
return View((object)role);
}
and in the corresponding Role_name.cshtml view you could use this value:
#model string
<div>The role is #Model</div>
Notice how it was necessary to cast the role variable to an object to ensure that the proper View method overload (taking a model) is being resolved.
On the other hand you could have used a view model which is always best practice:
public class MyViewModel
{
public string Role { get; set; }
}
which your controller action could have populated and passed to the corresponding stringly typed view:
public ActionResult Role_name(string role)
{
var model = new MyViewModel();
model.Role = role;
return View(model);
}
and in the view:
#model MyViewModel
<div>The role is #Model.Role</div>
Also I would recommend you taking a look at some of the getting started tutorials on the asp.net/mvc site.

ASP.NET MVC3: can Action accept mulitple model parameters?

For example:
class MyContoller
{
[MyCustomAttribute]
public ActionResult MyAction(ModelX fromRequest, ModelY fromSession, ModelZ fromCookie)
{
string productId = fromRequest.ProductId;
string userId = fromSession.UserId;
string cultureName = fromCookie.CultureName;
}
}
Reason:
I don't want to visit Request, Session and HttpContext in the controllers, and the default idea of MVC3 which passing models to actions is very great.
I want the number of parameters of MyAction is easy to change. For example, if I add a new parameter, the system will try to look for values in Request, Session or Cookies by the name or type of the parameter (I think custom ModelBinders may be required for cookie values) and pass the filled model to my action. I don't have to write extra code.
Can the custom attribute (MyCustomAttribute in the example) accomplish this idea?
I am not sure I follow you about the custom attribute. What are you expecting the custom attribute to do?
Yes, an action method can take as many model parameters as you want. Obviously, only one can be bound in any given request (because a view can only have one model). Whichever one is found first will be bound, and the others will be null.
So let's say you have the following:
public class ModelX {
public string X {get;set;}
}
public class ModelY {
public string Y {get;set;}
}
public class ModelZ {
public string Z {get;set;}
}
And you have an action method like this:
public ActionResult DoIt(ModelX x, ModelY y, ModelZ z)
{
return View();
}
And in your DoIt.cshtml you have the following:
#model ModelZ
#using(Html.BeginForm()) {
#Html.TextBoxFor(x => x.Z)
<input type="submit"/>
}
If you type something into the textbox and submit, then the model binder will bind a ModelZ with the value you entered and ModelX and ModelY will be null.
If you mean can an action method bind multiple models simultaneously, then I would have to ask you.. How exactly do you plan to have a view have more than one model? You can certainly create a wrapper model to contain the multiple models, but a view can only have one.
Create a composite ViewModel class that incorporates ModelX, ModelY and ModelZ. You can then populate an instance of your new ViewModel class, and pass that to your controller method.
public class XYZViewModel
{
public ModelX fromRequest { get; set; }
public ModelY fromSession { get; set; }
public ModelZ fromCookie { get; set; }
}
public class MyController
{
[MyCustomAttribute]
public ActionResult MyAction(XYZViewModel myModel)
{
string productId = myModel.fromRequest.ProductId;
string userId = myModel.fromSession.UserId;
string cultureName = myModel.fromCookie.CultureName;
}
}
You can always pass multiple parameters to your controller action, yes. The key is to make sure they are properly serialized in the request. If you're using a form, that means using the Html helper methods.
For example, let's say you want an action like this:
public ActionResult Multiple(ModelA a, ModelB b)
{
// ...
}
You could create simple partial view for each model:
#model MyProject.Models.ModelA
#Html.EditorForModel()
Then in your Multiple view, render the partial views like so:
#{ using (Html.BeginForm("Multiple", "MyController", FormMethod.Get))
{
#Html.Partial("A", new MyProject.Models.ModelA())
#Html.Partial("B", new MyProject.Models.ModelB())
<input type='submit' value='submit' />
}
I set the method to GET here so that you can easily see how MVC passes the parameters. If you submit the form, you'll see that MVC successfully deserializes each object.

MVC model binding naming convention for child objects?

I'm having trouble with default model binding naming convention when there is a child property. For example:
I have a ViewModel which looks something like this:
public class UserViewModel
{
public User BusinessObject { get; set; }
}
My User class has a property called "NetworkLogin"
My View has something like this:
<%: Html.LabelFor(model => model.BusinessObject.NetworkLogin)%>
<%: Html.TextBoxFor(model => model.BusinessObject.NetworkLogin)%>
Auto-Fill
And my controller, what I'd like to do, is
[HttpGet]
public ActionResult UserIndex(string networkLogin) { }
The problem:
The input parameter "networkLogin" is always null. This makes sense, because the actual parameter on the html element is name="BusinessObject.NetworkLogin" and id="BusinessObject_NetworkLogin". However, I don't know what parameter name I should use in my action method. I've tried "businessObject_NetworkLogin" and it doesn't work either.
However, I have this workaround that does work, but I don't like it. I add this to my ViewModel:
public string NetworkLogin
{
get
{
if (BusinessObject == null)
BusinessObject = new User();
return BusinessObject.NetworkLogin;
}
set
{
if (BusinessObject == null)
BusinessObject = new User();
BusinessObject.NetworkLogin = value;
}
}
And my View page now says this instead.
<%: Html.TextBoxFor(model => model.NetworkLogin)%>
Can someone tell me what the proper naming convention is for default model binding so that I don't have to employ the above workaround?
Thank you!
Indicate the prefix so that the model binder knows that the BusinessObject.NetworkLogin query string parameter actually refers to networkLogin which is what you use as action argument
public ActionResult UserIndex(
[Bind(Prefix = "BusinessObject")] string networkLogin
)
{
...
}
or reuse your view model:
public ActionResult UserIndex(UserViewModel model)
{
// TODO: use model.BusinessObject.NetworkLogin
// which is gonna be correctly bound here
...
}
As far as your workaround is concerned, once you put one of my two suggestions into action your view model property should really look like this:
public string NetworkLogin { get; set; }

How to set values for Model properties on .aspx page in ASP.NET MVC 2?

I have a strongly typed view. I get model passed into the view and then i assign model values to labels etc.
I would then also like to set Model values programmatically on .aspx page, like:
<%= Model.someValue = "foo"; %>
and then pass that model back to controller action and than access those values. I know that I can apply values to model like these:
<%= Html.TextBoxFor(n => n.someValue) %>
but in these case, this is not an option for me.
If the user is not supposed to modify the values of this model inside the view then you could use hidden fields or simply pass some unique identifier which will allow the controller action to retrieve back the model from the repository.
What information are you trying to set? You need to put them in form fields that will be POSTed back to the server. E.g.
public class MyModel
{
public string Name { get; set; }
public string UniqueCode { get; set; }
}
If can set properties if I need to:
<% Model.UniqueCode = "something"; %>
<%= Html.HiddenFor(m => m.UniqueCode) %>
And then accept those new values when the form is posted back:
public MyController : Controller
{
public Index()
{
return View(new MyModel { Name = "Hello" });
}
[HttpPost]
public Process(MyModel model)
{
string code = model.UniqueCode;
}
}
Though why is your view modifying the model?

Resources