MVC 4 - View inside a form - c#

I'm new to MVC. I have this view model:
public class ABCView
{
public string Name { get; set; }
public List<orders> Order { get; set; }
public int Version { get; set; }
public string Message { get; set; }
}
I want a view where the top of the form will contain Name and Message. In the bottom, I need only TWO orders side-by-side. How can I achieve this for Create/Edit operations. Any links will be helpful, thanks.

I would simply pass this model to a view, then output using #Html.TextBoxFor Name and Message where you want them. On the bottom of the form have a foreach loop which goes through the List and outputs the orders.
Great, simple example:
http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/examining-the-edit-methods-and-edit-view

Related

How to create MVC form from class with a List<T> property?

I am new to .NET MVC and am trying to figure out forms, I think I am on the right track but I am stuck trying to figure out how to create a form based off of a class that has an ObservableCollection<> of another class, for example:
public class Items
{
public string Notes { get; set; }
public ObservableCollection<LineItem> LineItems { get; set; }
}
and Line Items is something like:
public class LineItem
{
public string ItemNumber { get; set; }
public string QTY { get; set; }
}
From what I understand I should create an instance of Items in the controller and pass that to my view
return View(new Items());
Then on my view I use HTML helpers to create the form and when I submit the form, the model Items is sent back to the controller with filled out data. How do I go about having multiple LineItems in my view that bind to the ObservableCollection<> in my Model ? Let's say they need 2 LineItems, is there a programmatic way to show the inputs for public ObservableCollection<LineItem> LineItems multiple times, as an ObservableCollection is typically more than one instance of the class. I am using .NET MVC C# with Razor syntax.

Get MVC form field name with Html.FieldNameFor() defined by Orchard CMS

I want to generate form field names based on Html.FieldNameFor() but i'm not sure whether that works with my view model. The ViewModel looks like
public class FieldsModel
{
public string Caption { get; set; }
}
public class FormModel
{
public FieldsModel Fields { get; set; }
}
public class ViewModel
{
public FormModel Form { get; set; }
}
How do i write the Linq lamda expression so that Razor understands it?
Obviously something like
#Html.FieldNameFor(model.Form.Fields => Caption)
does not work.
Update:
To be clear, i need to get Caption from that function call.
Please try like below.
#Html.DisplayNameFor(model.Form.Fields.Caption)
It will gives the fields name exactly.

View that add items to nested list within nested list

I have a fairly simple example model that contains a list, which also contains a list.
// Main object ... 1
public class TestModel
{
public int Id { get; set; }
public string Name { get; set; }
// 0..*
public IList<Item> Items { get; set;}
}
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
// 0..*
public IList<SubItem> SubItems {get;set;}
}
public class SubItem
{
public int Id { get; set; }
public string Name { get; set; }
}
With these model classes, I would like to create a view which allow to dynamically add items and sub-items.
Example:
I have read a lot about this, but there does not seem to be a common method to perform what I want to do. There are lots of examples of a list within an object, but none about a list within a list within an object.
Is there a way to do what I want to do? Or do I absolutely need to separate my views?
What I've tried :
EditorTemplate (It works for my items list, but I can't add items to my SubItems list)
EditorFor (model => model.SubItems)
Add items with jQuery but I don't think it's the right way to do this.
Thanks,
S.H
This is possible, but the tricky part is getting the id's of your nested view controls to play nicely with the MVC model binder.
I've got this to work in the past by using the instructions detailed here --> http://mleeb.wordpress.com/2013/11/23/editing-nested-lists-in-asp-mvc-4/

How to show data from two ViewModels in one View

I would like to fill 2 divs in View with data from 2 ViewModels, but I have a problem.
My 2 viewModels:
public class ChatLogsNameTimeViewModel
{
public UserProfile UserProfile { get; set; }
public string Message { get; set; }
public DateTime Time { get; set; }
}
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public bool IsOnline { get; set; }
public virtual ICollection<ChatLogs> ChatLogs { get; set; }
}
Which means I want to show data from ChatLogsNameTimeViewModel in one div in View and data from UserProfile in other div in View.
This is my ViewModel that uses both viewModels above:
public class ChatLogsUsersViewModel
{
public IEnumerable<ChatLogsNameTimeViewModel> ChatLogs { get; set; }
public IEnumerable<UserProfile> Users { get; set; }
}
And this is my Index() action in controller:
var chatLogs = db.getChatLog().ToList();
var users = dba.getOnlineUsers().ToList();
var view = new ChatLogsUsersViewModel(chatLogs, users);
return View(view);
My problem is that I can not access to ViewModel attributes at all.
When I create foreach loop in view all I can access is this:
Which means I cannot access attributes at all to print them in foreach.
I have this in View:
#model IEnumerable<Chat.Models.ChatLogsUsersViewModel>
I assume that I am not doing something right in my controller. I have methods getChatLog() and getOnlineUsers() implemented in Model, they work alone no problem. I just don't know how to make them work together in one view.
You need to update the type of you view.
You are not passing the view a list of Chat.Models.ChatLogsUsersViewModel, you only have one and this model has two lists.
So update it to:
#model Chat.Models.ChatLogsUsersViewModel
Your model is strongly typed towards the wrong model. As Queti put it, your model should be typed #model Chat.Models.ChatLogsUsersViewModel.
As is, your model is attempting to access a collection of these models. You should find that if you do:
#for each (var x in Model) {
x.
}
x is then a single ChatLogsUsersViewModel and should display its properties in your dev environment. But again, this is not how you want to strongly type your model here.
If I'm not mistaken, this is what you are trying to do, and you can still access your data using a for each loop:
#model Chat.Models.ChatLogsUsersViewModel
for each (var log in Model._chatLogs) {
#<div>#log.Message</div>
}
...
for each (var user in Model._users) {
#<div>#user.UserName</div>
}

Passing ViewModel - View to Controller When JavaScript disabled

It's difficult for me for developping an functionality without JavaScript..
I have a ViewModel :
public class AccountRegisterViewModel
{
#region Properties
public User User { get; set; }
public ExternalAccounts ExtAccounts { get; set; }
public LocalPassword Password { get; set; }
public Company CompanyARegister { get; set; }
public Company CompanyBRegister { get; set; }
public bool SameCompanies { get; set; }
public int NbCompanies { get; set; }
...
}
In view, i have a link with checkbox for copying the first company with the second
But I don't know how pass this viewModel (View to Controller) for keeping my data and return the same View with the copie of company..
i try this, in view :
#Html.ActionLink("Click", "CopyCompanies","Account", new { model = Model })
#Html.CheckBoxFor(model => model.SameCompanies)
In Controller :
[AllowAnonymous]
public ActionResult CopyCompanies(AccountRegisterViewModel model)
{
...
if (model.SameCompanies)
{
// copie
}else //clear
...
return View("Step2Register", model);
}
Any idea ?? Thank you for your help
If the CopyCompanies action method requires the AccountRegisterViewModel object, then you will need to provide it. Unfortunately, you will not be able to provide the value using the approach you are following when creating the link.
Your two options would be to have a hidden field for each property in AccountRegisterViewModel and then let the model binding create the object, but even this would not be ideal, since the viewModel is composed of complex objects, so you would have way too many hidden fields.
Your second option, which I think is a better approach, would be to pass in some kind of Id that corresponds to the AccountRegisterViewModel that CopyCompany can use to look up the values it would need.

Categories

Resources