I have a droplist, and I would like to add an All as the first one.
How is that done with asp.net mvc?
I have:
<%= Html.DropDownList("selCustomerID") %>
works great, but I don't have an empty "all-option". I populate it like this:
return View("Index", new CustomerAdminEditViewModel { selCustomerID = new SelectList(_cs.GetCustomers(), "CustomerID", "CustomerName") });
Check this link : http://forums.asp.net/t/1429075.aspx
It is talking about an optional label parameter in the constructor.
Related
Filter records in index view using dropdown list . How replace xxxxxxx with selectedValue from dropdownlist to filter. I am not sure how to select the selectedValue from dropdown list in the view. The controller code and the view code is below. I know its a small problem. But I am trying to find for some hours.The filter is working fine if i replace with xxxxxx with 1 , 2 etc.But I want to change xxxxx with selectedValue from Dropdown list.
controller code
ViewBag.doctorsid = new SelectList(db.doctors, "doctorsid", "doctorsname",selectedValue);
return View(appointments.Where(d => d.doctorsid == xxxxxxx).ToList());
view code
#Html.DropDownList("Doctorsid","Select Doctor")
Use script given bellow
Script
$("#Doctorsid").change(function () {
var doctorsid = $('#Doctorsid').val();
var url = "/ControllerNmae/ActionResultName/+doctorsid ;
window.location.href = url;
});
Use a Model with variables for list and drop down value instead of passing list directly to view
Controller:
ViewBag.ClientTypeID = new SelectList(db.ClientType.Where(ct => ct.IsDeleted != true), "ID", "ClientTypeName");
View:
#Html.DropDownListFor(m => m.ClientTypeID, new SelectList(ViewBag.ClientTypeList, "Value", "Text"), new { #class="drop"})
I am using MVC and my requirement is that i need to build a action link with dynamic action route value.
for example.
In my model i have the list<string> so action link should be.
../ActionMethodName/ControllerName? Item[0]=model.item[0]&Item[1]=model.Item[1]...so on.
Any Suggestion is welcome.
Several ways.
1) You can pass as array and access it.
3) You can use foreach with razor syntex to generate this type of link.
Initally , the parameter needs to be passed are hardcoded than OnClick of somthing , replace the queryString values .
#Ajax.ActionLink("Test", "actionName", "ControllerName", new { foo = "foo1", name = "ABC" }, new AjaxOptions { UpdateTargetId = "TargetName" }, new { id = "Test" })
and
$("#btnTest").click(function () {
// Replace the hardcoded parameter value desired one ... :)
$("#Test").attr('href', $("#Test").attr('href').replace("foo1", "XXX"))
});
Note:- this solution i read from asp.net forums
http://forums.asp.net/t/1841591.aspx?+pass+Javascript+value+parameter+with+Ajax+ActionLink
I'm working on an MVC3/Razor page, and in my _layout I have
#RenderSection("relatedBooksContainer", false)
In another page I use that section with:
#section relatedBooksContainer
{
#{ Html.RenderPartial("~/Views/Shared/Bookshelf.cshtml", Model.Books);}
}
This doesn't work. From what I've read, RenderSection will only ever go one layer deep - it has no concept of the Html.RenderPartial in the related section and will just return a blank area. The workaround I read at http://forums.asp.net/t/1590688.aspx/2/10 is to use RenderPage and commit the returned HTML to a string, then outout that string in the render section...which works! That is, until I pass a model to the partial page, then it throws an error saying:
The model item passed into the
dictionary is of type
'TheBookshelf.ViewModels.BookshelfViewModel',
but this dictionary requires a model
item of type
'System.Collections.Generic.List`1[TheBookshelf.EntityModel.Book]'.
Anyone have any idea why this might be happening? Are there any other ways to achieve this?
Try the #Html.Partial instead
#section relatedBooksContainer
{
#{ Html.Partial("~/Views/Shared/Bookshelf.cshtml", Model.Books);}
}
The error message is regarding the type and return type of the Bookshelf from the model.
public IEnumerable<Book> Bookshelf()
{
var q = from book in bookshelf
select book;
IEnumerable<Book> myBooks = q.ToList<Book>();
return myBooks;
}
did the solution in the provided link work for you?
I couldn't get it to work. That is I couldn't get ViewData["MainView"] to pass the data from Layout.cshtml to partialview. This aparently is a feature as every view is supposed to have it own ViewData obj. It seems ViewData is not global like I have thought. So what I get in ViewData["MainView"] from Layout in my partial view is null......I eventually found a work around for this and was able to pass the page reference from Layout to Partialview via a #Html.Action call from Layout -> Controller -> PartialView. I was able to get my partialview to access and write to the correct rendersection. However I want to call the same partialview many times in my Layout.cshtml. A subsequent call to the same Partialview again in the Layout, does not work, as the reference to layout has changed since the first call and rendersection update. So the code looks like this:
Layout.cshtml:
#RenderSection("Top", false)
#Html.Action("Load", "Home", new { viewname = "_testPartialView", pageref = this })
#Html.Action("Load", "Home", new { viewname = "_testPartialView", pageref = this })
Partial View:
#Model Models.testModel
#Model.Content
#{
var md = (System.Web.Mvc.WebViewPage)#Model.pageRef;
#*This check fails in subsequent loads as we get null*#
if(md.IsSectionDefined("Footer")) {
md.RenderSection("Footer");
}
else {
md.DefineSection("Footer", () => { md.WriteLiteral("<div>My Contents</div>"); });
}
}
}
controller:
public ActionResult Load(string viewname, System.Web.Mvc.WebViewPage pageRef)
{
var model = new Models.testModel { Content = new HtmlString("time " + i++.ToString()), pageRef = pageRef };
return PartialView(viewname, model);
}
In my view the call below generates url ending with Tasks/Edit but I want it to generate url like Tasks/Edit/23
<%= Html.ActionLink<TaskController>("Edit Task", (x) => x.Edit("23"))%>
in Global.asax:
string taskController = NameResolver.NameOfController<TaskController>();
string editAction = NameResolver.NameOfAction<TaskController>(x => x.Edit(null));
routes.MapRoute(
"EditTasks",
"Tasks/Edit/{id}",
new { controller = taskController, action = editAction, id = string.Empty });
I also have binding problem in this action. Values set from view is not binded to my Edit parameter. It comes null everytime and I have not set DefaultModelBinder anywhere. Here is the Edit action:
public ActionResult Edit (string id)
{
//retrieve some data and pass it to view and return view
}
So what can be the problem here? How can I solve url and binding problem? And yes I am a Asp.Net Mvc beginner :)
<%= Html.ActionLink("Task", "Edit", new { id = "2" }) %>
Although why is your id a string and not an int?
Is there a way to include a default empty option (or with text) if there is no selected value for a dropdownlist?
The below will prepend string.Empty to the SelectList (or IEnumerable) specified in the ViewData["Menu"] item. The select will have id and name MenuID.
<%= Html.DropDownList( "MenuID",
(IEnumerable<SelectListItem>)ViewData["Menu"],
string.Empty ) %>
Documentation: DropDownList method
For Example:
Controller :
private void InitScreenInfoCollate()
{
IEnumerable<MstBrd> listbrd = ibrandRepository.GetItemsByUsercode("");
ViewBag.Brands = new SelectList(listbrd, "brd_cod", "brd_mei", null);
}
View :
#Html.DropDownList("Brands", null, string.Empty, new { #class = "form-control"})
Result :
This easy solution worked for my mvc5 project:
in view:
#{
Model.ModelItemsList.Add(new ModelItem{ });
SelectList modelItemSelectList = new SelectList(Model.ModelItemsList, "ModelItemID", "ModelItemName");
}
Just add a new item to the List<> you want to display in your view.
In my case, I added a empty "ModelItem" to my List<ModelItem> ModelItemList. Since my ModelItemID is a Guid, I had to check for Guid.Empty in my controller method and do some code.
Thats all.
The solution presented here worked very well for me: http://forums.asp.net/t/1142484.aspx/1
The basic idea is that you set the AppendDataBoundItems property of your DropDownList to true and then put an asp:ListItem in the DropDownList and that will become your default item with all the databound items coming after it.