Posting with multiple models in a view - c#

So I have a View that contains 2 models. Each model has its own form and submit button.
Currently, I have both submit buttons are processed by the same controller method and use reflection to figure out which model type was passed. But it seems like there would be a better way... any ideas?
I have something like this:
Models:
public class Model1
{
// Elements
}
public class Model2
{
// Elements
}
Controller:
public ViewResult ConMeth(Object model)
{
Type t = model.GetType();
if(t == typeof(Model1)
{
// Do work for Model1
}
else if(t == typeof(Model2)
{
// Do work for Model2
}
else
{
// Do something else...
}
}

If you show your view info, I suspect you've got two seperate things happening in the view. Just put each thing in it's own form and use the
#using (Html.BeginForm(...)){}
and specify the actions by name and the controller (if necessary) in the BeginForm params... That should get rid of the ambiguous reference error
Here is an example w/ the older (not razor) tags

You can use a Tuple<> in your view to have two view models, and then in the #Html.BeginForm() helper method for each form, you can specify POSTs to two different controllers to process your form data.
#model Tuple<ProjectName.Models.Model1, ProjectName.Models.Model2>

Related

How to check if a List in a ViewBag contains string

I have a ViewBag that is a list which i create in the controller:
List<string> listoflists = new List<string>();
it then gets filled with some string like for example:
listoflists.Add("java");
listoflists.Add("php");
listoflists.Add("C#");
Adding it to the ViewBag:
ViewBag.listoflists = listoflists;
i then would like to check in my View if this contains a specific string:
#if(ViewBag.listoflists.contains("java")
{
// do html stuff
}
but when running this i get the following error:
System.Collections.Generic.List does not contain a definition for contains
What am i doing wrong, or how should i check if a list contains something in the View?
You might need to cast back to a list of strings:
#if (((IList<string>)ViewBag.listoflists).Contains("java")
{
// do html stuff
}
Also notice that the Contains method starts with a capital C.
So as you can see using ViewBag in ASP.NET MVC is a complete crap leading to very ugly code in your views. For this reason it is strongly recommended to use view models which will contain all the necessary information of a specific view and also being strongly typed.
So cut this ViewBag crap and start using view models:
public class MyViewModel
{
public IList<string> ListOfLists { get; set; }
}
that your controller action can populate and pass to the view:
public ActionResult Index()
{
var model = new MyViewModel();
List<string> listoflists = new List<string>();
listoflists.Add("java");
listoflists.Add("php");
listoflists.Add("C#");
model.ListOfLists = listoflists;
return View(model);
}
and now you can have a strongly typed view to this model which will allow you to avoid the previous casting:
#model MyViewModel
#if (Model.ListOfLists.Contains("java"))
{
// do html stuff
}
So basically every time you use ViewBag/ViewData in an ASP.NET MVC application an alarm should immediately ring in your head telling you: Man, what the hell, you are doing it wrong. Just use a view model in order to avoid transforming your views into an abominable mess of completely irrelevant C# language constructs like casting and stuff. Views are meant for displaying markup.
ViewBag is a dynamic type, it does not know at compile time what is the actual type contained in the key ViewBag.listoflists you defined, you need to cast it to specific type and then you can call those methods on it:
#{
List<string> languages= ViewBag.listoflists as List<string>;
}
#if(languages.contains("java")
{
// do html stuff
}

Combine controller and action parameter for ActionLink / RedirectToAction

My application has a view that is linked from various different other views located in different controllers. I would like to have a 'Back' button in this view that will send the user back to the previous view, which ever that may be.
To this end I have added a string attribute to the viewmodel which I would like to use to reference the originating view /MyController/MyAction in the #Html.ActionLink parameters.
Some of the views linking to this view belongs to current controller, some belong to other controllers. This means I have to pass the controller as well as the action to the ActionLink.
As it stands, my code looks something like this:
ViewModel:
public class MyViewModel
{
public int MyData { get; set; }
[HiddenInput]
public string ReturnUrl { get; set; }
}
View:
#Html.ActionLink("Back", Model.ReturnUrl)
This produces the undesirable result of localhost:####/CurrentController/MyController/MyAction
Of course I could always save two strings on the ViewModel (one for the controller and one for the action) and pass them to the ActionLink seperately, but if possible I would like to avoid that. Is there an overload of ActionLink that allows me to use a single return url string, without making implications about the controller?
Also, is it possible to achieve the same thing on the controller side, f.ex. like this:
[HttpPost]
public ActionResult DoStuff(MyViewModel model)
{
// do stuff
return RedirectToAction(model.ReturnUrl);
}
You shouldn't use ActionLink. Just use an anchor tag, like this:
<a href='#Url.Content(Model.ReturnUrl)'>Back</a>
And, in your Action, you can do it like below:
[HttpPost]
public ActionResult DoStuff(MyViewModel model)
{
// do stuff
return Redirect(model.ReturnUrl);
}
Also, another solution would be to have two properties (ReturnController and ReturnAction) in your model.

How to validate multiple ViewModels in single view by condition using ModelState.IsValid

I have a problem with validation of one view with multiple ViewModels. My situation is, that I have one Basic form, which is same for lot of pages. By ID parameter, I render new external fields to this Basic form. These external fields are type of ActionResult, using own ViewModel and own Controller. In Main controller on Post action I want to control if ModelState.IsValid, but I have problem - it validate all ViewModels of all external fields, but I want to validate only ViewModel of active external fields (and Basic form too).
It looks like this:
ViewModel of all view models
public class AllFieldsVm
{
public BasicFormVm BasicFormVm { get; set; }
public ExternalFieldXyVm ExternalFieldXyVm { get; set; }
public AnotherExternalFieldVm AnotherExternalFieldVm { get; set; }
}
In controller of external fields I create new instance of AllFieldsVm and in this create new instance of ExternalFieldXyVm (if I need, I prefill these fields). This I render whitout layout like partial view (using #{Html.RenderAction("Action", "Controller", new {#someOptionalData = value});} ), when some condition is true.
In controller of Basic form on Post action I have something like this and I want to use something like this code if (ModelState.IsValid(model.BasicFormVm) && ModelState.IsValid(model.ExternalFieldXyVm)):
[POST("someurl-id{someId}")]
public ActionResult SaveFormData(int someId, AllFieldsVm model)
{
//Here I want something like
//if (ModelState.IsValid(model.BasicFormVm) && ModelState.IsValid(model.ExternalFieldXyVm)) or something like that...
var se = new SomeEntity();
se.property1 = model.property1;
se.property2 = model.property2;
using (var dbc = _db.Database.BeginTransaction())
{
try
{
_db.Add(se);
_db.SaveChanges();
//My Condition - when save external data
if (someId == (int) MovementTypes.SomeEnumInt)
{
var rd = new ExternalFieldEntity
{
PropertyA = se.property0,
PropertyB = Convert.ToDateTime(model.ExternalFieldXyVm.SomeExternalFieldName)
};
_db.Add(rd);
_db.SaveChanges();
}
dbc.Commit();
}
catch (Exception)
{
dbc.Rollback();
}
}
return RedirectToAction("Action", "Controller");
}
So, my question is, how can I validate ExternalFieldXyVm separatly based on some conditions?
Is it possible, or I have to create all own validators, without using basic DataAnnotations or FluentValidation? I have no experience with these types of forms, so please be patient...
Thanks to all for help!!
Great, I got it. I play with this for two days, don't know how it is possible that I didn't see that.
Result is: When view with own view model which is included in main viewmodel, isn't rendered into view, this viewmodel is not validate on post action. So my Basic form is validate everytime, and ExternalFields are validate only when are rendered. So sorry, for so stupid question....

using View with layout when both require specific Model

I`m writing an ASP.Net MVC application with Razor.
Assume that I have HomeController and some views for it.
1. View1
2. View2
3. View3
All this views use common _MyLayout file, which should look like this:
When the links are clicked, the views are rendered by RenderBody() method.
Each view is strongly typed: it requires its own Model.
Everything was fine untill I decided to add special Model to _MyLayout view.
But now I get error
The model item passed into the dictionary is of type 'TestUp.Models.UserModels.PendingTestsModel', but this dictionary requires a model item of type 'TestUp.Models.UserModels.UserNavigationModel'.
Here is controllers code
public ActionResult View1()
{
ModelForView1 model = new ModelForView1();
return View(model);
}
public ActionResult View2()
{
ModelForView2 model = new ModelForView2();
return View(model);
}
public ActionResult View3()
{
ModelForView3 model = new ModelForView3();
return View(model);
}
Shortly speaking if layout view doesn`t require model, specific method for View is invoked,
model is created, passed to view and everything is ok. But now layout requires model as well so it crashes.
The question is: how do I elegantly resolve this problem?
Desired workflow is:
View1 is requested
Method in controller for this view is called, model instance created, passed to view
Some method for layout is called, model for layout created, passed to layout.
Is it possible to make things work somehow like this?
Thanks.
Create a base model type and have your specific view models extend it. This base model can have a property of type UserNavigationModel. The layout can accept the base model and use the new property as the model for the navigation menu.
public abstract class ModelBase
{
public UserNavigationModel NavigationModel { get; set; }
}
public class ModelForView1 : ModelBase { ... }
public class ModelForView2 : ModelBase { ... }
public class ModelForView3 : ModelBase { ... }
View1:
#model ModelForView1
Layout:
#model ModelBase
#* use Model.NavigationModel for nav bar *#

Why does TryUpdateModel return false even though properties are set and valid?

I have web application based on ASP.Net MVC3. I have a need for a "Create" view which will not know the model type until the user picks a sub-type to create from a drop-down. To attempt to solve this problem, I have created an editor template under Shared/EditorTemplates for each derived model type. This allows me to create a single "Create.cs" which is strongly-typed to a view model. The view model only has two members, an enum and a complex type. The idea is that the view will initially show only a drop-down (editor for the enum member) then when the user initially submits the specified "model type" (drop-down selected value), the POST action can check the "model type" specified and instantiate the correct derived model type for the view model's single complex member who's type is the base type for all possible "model types".
The abstract + derived type model objects...
public abstract class MyModelBase
{
public MyModelType_e ModelType {get; set; }
[Required]
public string Name { get; set; }
}
public class DerivedType1 : MyModelBase
{ ... }
public class DerivedType2 : MyModelBase
{ ... }
public class DerivedType3 : MyModelBase
{ ... }
I have a complex view model as follows...
public enum MyModelType_e
{
DerivedType1 = 0,
DerivedType2 = 1,
DerivedType3 = 2
}
public class MyModelCreate
{
public MyModelType_e ModelTypeForSelectList { get; set; }
public MyModelBase ModelBase { get; set; }
}
My GET controller action instantiates the above view model for the view i.e., only a drop-down list displayed with items based on the MyModelType_e enum + the value of the model's "ModelBase" property is initially null. So the GET action method looks like this...
[HttpGet]
public ActionResult Create()
{
return View(new MyModelCreate());
}
Note the comment in caps bellow regarding the crux of my issue which is that TryUpdateModel fails (see below) even though it sets the properties of the ModelBase (derived-type) member to the corresponding form values as expected...
[HttpPost]
public ActionResult Create(MyModelCreate model)
{
if (model.ModelBase == null ||
(int)model.ModelTypeForSelectList != model.ModelBase.ModelType)
{
switch (model.ModelType)
{
case MyModelType_e.DerivedType1:
model.ModelBase = new DerivedType1();
break;
case MyModelType_e.DerivedType2:
model.ModelBase = new DerivedType2();
break;
case MyModelType_e.DerivedType3:
model.ModelBase = new DerivedType3();
break;
}
return View(model);
}
if (!TryUpdateModel(model.ModelBase))
return View(model); // <<< THIS HAPPENS EVEN THOUGH ModelBase APPEARS TO BE UPDATED PROPERLY...
// For instance, I can see right here with intellisense that model.ModelBase.Name
// is NOT null or empty but rather is truly updated with the correct form value(s)...
// TODO: Insert the record, etc... (currently we never get here...)
}
So the above section is where the problem stems from but here is my view to help understand...
#model MyNamespace.MyModelCreate
<h2>Create</h2>
...
#using (Html.BeginForm())
{
#Html.ValidationSummary(false)
<fieldset>
<legend>Input</legend>
<div class="editor-label">
#Html.Label("Select Model Type")
</div>
<div>
#Html.EnumDropDownListFor(model => model.ModelType)
#Html.ValidationMessageFor(model => model.ModelType)
</div>
#*
Conditionally show the correct editor template...
There is one existing under ../Shared/EditorTemplates for each
derived type (DerivedType1, DerivedType2, DerivedType3, etc...)
This much is working in the sense that the correct editor fields
are displayed based on what the user selects in the above drop-down.
*#
#if (Model.InputModel != null)
{
#Html.EditorFor(model => model.ModelBase);
}
<p>
<input type="submit" value="Continue" />
</p>
</fieldset>
}
So once with the initial POST (model type is selected), my POST action method falls through to the TryUpdateModel line by design but for some reason the validation fails. The part I really don't understand is that the validation summary reports "Name is required" even though I can clearly watch TryUpdateModel set the Name propery properly on the ModelBase member of the view model.
I'd greatly appreciate any help or guidance here... I am fairly new to ASP.NET MVC and I am open to doing things differently since I understand there are probably other ways I could design my requests/actions + views to accomplish this "multi-step" problem but I really am just going for the simplest possible thing which I why I like the EditorTemplate approach for handling the derived model types, etc.
Thanks in advance...
Check ModelState. There should be errors set after TryUpdateModel runs.
I had to remove a Required attribute from some properties.
My solution basically involved growing two additional controller actions (GET and POST). I created separate views under ../Shared/EditorTemplates which are strongly typed (one for each derived model type). This way the initial "Create" POST action only receives the selected type (model is just an enum value specifying the desired type) and then redirects to another controller action like "CreateByType" which gets a new instance of the requested concrete type and returns the correct edit view. The additional "CreateByType" controller actions (GET and POST) only needs to deal with the abstract base because it requests the instance from the service layer (passing the enum value). Then all I had to do was create a view under EditorTemplates for each concrete type.

Categories