Passing List from View to Controller - MVC4 - asp.net-mvc-4

I am now kind of stuck here.
I have my Models here
namespace DSS.Models
{
public class SupplierItems
{
public string GroupID { get; set; }
public string GroupName { get; set; }
public List<SupplierLineItems> LineItems { get; set; }
}
}
and have Properties of Supplier Items are defined in Model as
namespace DSS.Models
{
public class SupplierLineItems
{
public string Frequency { get; set; }
public string UnitPrice { get; set; }
public int Index { get; set; }
}
}
My Controller is just hardcoding the Supplier Items and line items
namespace DSS.Controllers
{
public class HomeController : Controller
{
[HttpPost]
public ActionResult GetLineItems(SupplierItems SupplierItems)
{
return View();
}
public ActionResult Index()
{
List<SupplierLineItems> ListOfLineItems = new List<SupplierLineItems>();
SupplierLineItems LineItems = new SupplierLineItems();
LineItems.Frequency = "Regulate";
LineItems.UnitPrice = "20";
LineItems.Index = 1;
ListOfLineItems.Add(LineItems);
SupplierLineItems LineItems1 = new SupplierLineItems();
LineItems1.Frequency = "Regulate";
LineItems1.UnitPrice = "20";
LineItems.Index = 2;
ListOfLineItems.Add(LineItems1);
SupplierItems Items = new SupplierItems();
Items.GroupID = "1";
Items.GroupName = "Core Group";
Items.LineItems = ListOfLineItems;
return View(Items);
}
}
}
Here is my View
#model DSS.Models.SupplierItems
#using DSS.Models
#{
ViewBag.Title = "Home Page";
{
<a type="submit" href="#Url.Action("GetLineItems", "Home", Model)">#Model.GroupName</a>
<br />
foreach (var item in Model.LineItems)
{
#item.Frequency<br />
#item.UnitPrice<br />
#item.Index<br />
}
}
}
When I click on Anchor tag of my view and pass the Model to "GetLineItems" of Home Controller, My Supplier items get passed successfully but List of Line items always comes as null. I am not sure what is going wrong . Anyone, please help
enter code here

You should use ajax post to the controller or simply use form submission to achieve this
instead of using url.action which is an url form of submission

Related

Asp.net MVC , CheckBoxs not binding in case of list

Though it seems like it has been discussed earlier also , but I could not find the solution -:
I have a VM like this-:
public class RoomsVm
{
public RoomsVm()
{
Rooms.Add(new Room()
{
IsVip = true,Id = "1"
}
);
Rooms.Add(new Room()
{
IsVip = false,
Id = "2"
}
);
}
public List<Room> Rooms=new List<Room>();
}
public class Room
{
public bool IsVip { get; set; }
public string Id { get; set; }
}
My controller has 2 simple actions- 1- for get and 2 for post-:
public ActionResult IndexView2()
{
var roomVM = new RoomsVm();
return View(roomVM);
}
[HttpPost]
public ActionResult IndexView2(RoomsVm roomsVm)
{
return View(roomsVm);
}
In my view - I simply display my rooms with check boxes like this-:
#model MVCApplication.Models.RoomsVm
<h2>IndexView2</h2>
#using (Html.BeginForm())
{
for (int i = 0; i < Model.Rooms.Count(); i++)
{
#Html.CheckBoxFor(modelItem=>modelItem.Rooms[i].IsVip,new{value=Model.Rooms[i].Id})
}
<input type="submit" value="Submit"/>
}
Problem- When I click Submit button, I don't get updated values of rooms checkboxes in input value of Post action ..
What wrong am I doing ?
You don't have accessors on your Rooms property
public class RoomsVm
{
public RoomsVm()
{
Rooms = new List<Room>(); // Initialize rooms
Rooms.Add(new Room() {.... //Add rooms
}
public List<Room> Rooms { get; set; } // Add get/set
}

ASP.NET MVC Populating dropdownlist

I've been trying to populate a dropdownlist for a while now and would appreciate some help. I have my model and viewmodel and my trying to populate the Dropdownlist and send it to the view so a user can choose a cartype and click submit.
public class Cars
{
public int CarId { get; set; }
public string Name { get; set; }
}
public class CarViewModel
{
public int SelectedCarId { get; set; }
public IEnumerable<SelectListItem> CarTypes;
}
public ActionResult FillDropDown()
{
var model = new ViewModel();
model.CarTypes = (from s in context.CarTypes
select new SelectListItem()
{
Text = s.Name,
Value = SqlFunctions.StringConvert((double)s.Id).Trim(),
}).ToList<SelectListItem>();
return View(model);
}
So I would like some help how to render this in the view. I tried the following but I get a nullreference exception.
#Html.BeginForm("FillDropDownList","Home", FormMethod.Post,null)
{
#Html.DropDownListFor(x => x.SelectedCarId, Model.CarTypes);
<input type="submit" value="submit" />
}
Try to use CarViewModel instead of ViewModel.
public ActionResult FillDropDown()
{
var model = new CarViewModel(); //CarViewModel instead of ViewModel
model.CarTypes = (from s in context.CarTypes
select new SelectListItem()
{
Text = s.Name,
Value = SqlFunctions.StringConvert((double)s.Id).Trim(),
}).ToList<SelectListItem>();
return View(model);
}
EDIT:
Change your IEnumerable property in CarViewModel into SelectList
public class CarViewModel
{
public int SelectedCarId { get; set; }
public SelectList CarTypes;
}
Make sure the SelectListItem is not null, in the FillDropDown() method.

MVC 4 model values not passed to controller action

I have view and want to display content from the object stored in session one by one when user clicks on NEXT button. View uses WorldModel object to display the data. Id is hidden field and Content should be displayed on page.
View:
#model MvcApplication4.Models.WorldModel
#{
ViewBag.Title = "View1";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>View1</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary()
<fieldset>
#Html.HiddenFor(model => model.Id)
#Html.DisplayFor(model => model.Content)
<input type="submit" value="Next" />
</fieldset>
}
Model: This class contains two properties id and content. Basically id is mapped to candidate id and Content is mapped to candidate name.
namespace MvcApplication4.Models
{
public class WorldModel
{
public int Id { get; set; }
public string Content { get; set; }
}
}
Controller: Class has two action methods one doesn’t take any parameter and other takes WorldModel as parameter.
namespace MvcApplication4.Controllers
{
public class WorldController : Controller
{
//
// GET: /World/
public ActionResult View1()
{
List<Candidate> obj = new List<Candidate>();
obj.Add(new Candidate() { Id = 1, Name = "ABCD", IsNameDispalyed = false});
obj.Add(new Candidate() { Id = 2, Name = "PQR", IsNameDispalyed = false });
obj.Add(new Candidate() { Id = 3, Name = "XYZ", IsNameDispalyed = false });
CandidateSession cs = new CandidateSession(){Candidates=obj};
Session["Can"] = cs;
return View();
}
[HttpPost]
public ActionResult View1(WorldModel worldModel)
{
CandidateSession cs = (CandidateSession)Session["Can"];
var can1 = cs.Candidates.Where(x => x.IsNameDispalyed == false).First();
can1.IsNameDispalyed = true;
return View(new WorldModel() {Id=can1.Id, Content=can1.Name });
}
}
public class Candidate
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsNameDispalyed { get; set; }
}
public class CandidateSession
{
public List<Candidate> Candidates { get; set; }
}
}
When I access URL as “../World/View1” it fills data to session object and click on NEXT button calls the action public ActionResult View1(WorldModel worldModel). In the method I get the object whose info is not displayed return the view with WorldModel object with id and Content. Now first time the Content is displayed but when the I click on NEXT button it call action public ActionResult View1(WorldModel worldModel). But the id and Content values of worldModel object are null. Why data is null, I set the values in the previous call?

"inline" editing in a mvc 4 list of objects

I have a strange problem and I don't know if this is actually possible.
What I want is, to be able to list all the values from my model and and edit them directly in the list.
Here's what I have:
Model Linker:
public class StoreLinkerModel
{
//public Guid? id { get; set; }
public IEnumerable<Stores> StoresAndOpeninghours { get; set; }
}
public class Stores
{
public long ID { get; set; }
public string StoreName { get; set; }
public string Address { get; set; }
public string Zip { get; set; }
public string City { get; set; }
}
My Controller:
public ActionResult Overview()
{
var model = new StoreLinkerModel
{
StoresAndOpeninghours = new[]
{
new Stores()
{
ID = 0,
Address = "Enghavevej 70"
},
new Stores()
{
ID=1,
Address = "Løngangsgade 30"
},
}
};
return View(model);
}
[HttpPost]
public ActionResult Overview(StoreLinkerModel model)
{
if (ModelState.IsValid)
{
var x = "go go go";
}
return RedirectToAction("Overview");
}
My overview.aspx page:
#model streetoffrs.web.Models.StoreLinkerModel
#{
ViewBag.Title = "Overview";
Layout = "~/Views/Shared/_dashboard.cshtml";
}
#Html.EditorFor(x => x.StoresAndOpeninghours)
and my EditorTemplate stores.aspx
#model streetoffrs.web.Models.Stores
#using (Html.BeginForm("Overview", "Dashboard", FormMethod.Post, new { name = "id" + #Html.DisplayFor(m => m.ID) }))
{
#Html.EditorFor(x => x.Address)
<input type="submit" class="left btn btn-primary" value="Ret butiksdata">
}
<br />
The list is being generated as it should, and when I hit the first button at the first editorfor it will post the model to my controller, but when I push the 2nd button, the model is null, but the first button still works!
Is this possible at all, if yes what am I missing, if not, tell me how I can accomplish this.
thanks in advance!
you need edit post action like this:
[HttpPost]
public ActionResult Overview(StoreLinkerModel model)
{
if (ModelState.IsValid)
{
var x = "go go go";
}
return View(model);
}
the RedirectToAction will be go to the first Overview Action,so you will be lost the data.

Poll System in ASP.NET MVC

I want to Display Polling in section of My Page, I have created these POCO classes for do that :
public class Polls
{
public int Id { get; set; }
public string Question { get; set; }
public bool Active { get; set; }
public IList<PollOptions> PollOptions { get; set; }
}
public class PollOptions
{
public int Id { get; set; }
public virtual Polls Polls { get; set; }
public string Answer { get; set; }
public int Votes { get; set; }
}
And I have Used below ViewModel :
public class PollViewModel
{
public int Id { get; set; }
public string Question { get; set; }
public string Answer { get; set; }
}
Then, I passed my model using above ViewModel to my View :
public ActionResult Index()
{
var poll = from p in db.Polls
join po in db.PollOptions on p.Id equals po.Polls.Id
where p.Active == true
select new PollViewModel {
Id=p.Id,
Question=p.Question,
Answer=po.Answer
};
return View(model);
}
in my View I want to display Question and Answer of my Poll, I have tried this code :
#section Polling{
#foreach (var item in Model.Polls)
{
<input type="radio" /> #item.Answer
}
}
above code works correctly but I want to display Question too, something like this :
#section Polling{
**#Model.Polls.Question**
#foreach (var item in Model.Polls)
{
<input type="radio" /> #item.Answer
}
}
How can I do that?
PS: I have one row in my Polls Table for display in Home Page
There is relationship between Polls and PollsOption. So get Polls from your db. And pass it to view. Also you already have PollsOptions that connected to to their Polls. No need to join two tables.
controller
public ActionResult Index()
{
// get active Polls
var poll = from p in db.Poll
where p.Active == true
select p;
// pass it to the view
return View(poll);
}
view
#model IEnumerable<Polls>
#section Polling{
#foreach (var question in Model)
{
<h2>#question.Question</h2>
#foreach(var answer in question.PollOptions)
{
<input type="radio" /> #answer.Answer
}
}
}