WebMatrix UrlData collection not getting filled in MVC - model-view-controller

I have an MVC 3 application which employs WebMatrix in one of its pages.
I have something like this in the page:
string s1 = UrlData[0];
var query = "select * from test where id = " + UrlData[1];
// Open the database of the service delivery
var dataBase = Database.Open("connection_string");
var results = database.Query(query);
There is an MVC Dropdownlist in the page and in the onchange event of this dropdownlist i am submitting the MVC form. It goes to the controller method which has this dropdownlist value as the parameter and after some operations is redirected back to the same page using RedirectToAction().
But, the UrlData collection does not have any values. Could some one help me out with this?
Thanks
Nidhin

UrlData is sorta like a Querystring with the execption that each value is located by position not by name. So if you where to have Url like http://localhost:6324/A/B/C/D in you Default.cshtml page and you did the following code:
<p>Total number of items in UrlData: #UrlData.Count</p>
<ul>
#for(var i = 0; i < UrlData.Count; i++){
<li>UrlData[#i]: #UrlData[i]</li>
}
</ul>
the output would look like
UrlData[0] : A
UrlData[1] : B
UrlData[2] : C
UrlData[3] : D
To clarify even further here is a "fantastic" article on the subject
UrlData

Related

In JSP how do I make a GET request on selection of one dropdown value and get data for other dropdowns from DB?

Hi All please suggest me some approaches for the below problem
Spring boot MVC: UI page is JSP
Currently All dropdown values are getting from DB when the home page is opened
I am stuck at the below task
On selection of one dropdown value, the below other dropdown values should be auto populated in jsp before the form is submitted
How do I make a GET request on selection of one dropdown value and get data for other dropdowns from DB?
below is the sample jsp form
enter image description here
if you using jquery with jsp then , use Ajax POST in Change event of name-3 dropdown , on success function populate on name-4 , name-5 drop down.
Like,
$(document).on('change','#name3',function(){
callAjaxByObj({
"url" : "url",
"type" : "POST",
"data" : {"name3Id" : $(this).val()},
"resp" : afterSuccess(),
"error" : ajaxFailed()
});
});
function afterSuccess(){
return function(resp){
// here you can populate Selected data on name-4, name-5 dropdowns use below
$("#name4").val(resp.name4Id);
$("#name5").val(resp.name5Id);
// OR if you want to append new drop down values to append name4, name5 dropdowns
var htm='';
var index = 0;
var obj = '';
for(index = 0; index < resp.name4List.length; index++) {
obj = resp.name4List[index];
htm +='<option value="'+obj.id+'" >'+obj.name+'</option>';
}
$("#name4").append(htm);
// same way you can do for name5 drop down.
}
}

Adding extra url query string to #Html.PagedListPager aside page in ajax call

I am PagedList for pagination when collecting values to my web page through ajax. The ajax call works well but When I click on the pagination links to display other details of the report for other pages, I have no value returned because I am adding extra parameters to the #Html.PagedListPager method aside the page used in the action method.
I am returning values through my partial views. The problem is how to add the extra
parameters to the pagination link.
My partial view code is below
#using Microsoft.Security.Application
#using PagedList
#using PagedList.Mvc
#model IPagedList<LiveChatPrototype.Mvc.Models.Livechat.Chatreportview>
#{
var FromDate = ViewBag.FromDate; //values to be added to pagination link
var ToDate = ViewBag.ToDate; //values to be added to pagination link
}
#if (Model != null)
{
<div class="numlinks">
#Html.PagedListPager(Model, page => Url.Action("DisplayAllchatreport",
new { FromDate = FromDate, ToDate = ToDate, page = page }), new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation = true, DisplayItemSliceAndTotal = true })
</div>
}
Please focus on the bottom part of the code, cause that is were the problem lies adding the ViewBag values to be part of the url so the pargination link is clicked other details can be returned.
FromDate and ToDate values were null. So I edited my controller and passed this values into the model which I was sending into my partialView.
So this allowed me to use them in my pagination links in the partialView
use Request to manage
fromDate = Request.QueryString["fromDate"]
//...

display the selected value from DropDown in a page

Hi Folks i am retrieving Name from database to drop down list. Now i want to display the selected value from DropDown in a page. My code is like follows to retrieve data,am using Fluent data as a framework.
My Controller Action is
var query = Abc.GetProducts();
ViewBag.EnterpriseId = new SelectList(query.AsEnumerable(), "EnterpriseId", "Name");
return View();
My view is ,
#Html.DropDownList("EnterpriseID", (SelectList) ViewBag.EnterpriseID, "--Select Resource--")
You can use a simple js for this:
elem = $('.selectClass');
options = elem.find('option[value="'+DataFromDBHere+'"]');
if(options){
options.prop('selected', 'selected');
}

mvc3 create link from controller

Is there a way to create a link from a controller and apppend it to a ViewBag ? I been trying but nothing is working
HyperLink hyperl = new HyperLink();
hyperl.NavigateUrl = "http://www.example.com/";
hyperl.Text = "Here is why..";
ViewBag.check ="You must register"+ hyperl;
You must register System.Web.UI.WebControls.HyperLink
I know that I am probably only missing 1 more line but i can't find it..
You could use the TagBuilder and do something like
public static TagBuilder CreateAnchor(string text, string url)
{
var anchor = new TagBuilder("a");
anchor.SetInnerText(text);
anchor.MergeAttribute("href", url);
return anchor;
}
Then pass to the ViewBag Like this
ViewBag.hyperl = CreateAnchor("Here is why..","http://www.example.com/");
You're mixing web forms with MVC. The result will be an abomination. Just send the URL through the view bag, and use it in the markup in a plain anchor tag:
Controller code:
ViewBag.LinkAddress = "...";
Markup:
Link
Try this
in Controller
HyperLink hyperl = new HyperLink();
hyperl.NavigateUrl = "http://www.example.com/";
hyperl.Text = "Here is why..";
ViewBag.check = hyperl.NavigateUrl;
ViewBag.Text = "You must register";
in View
<a href=#ViewBag.check>#ViewBag.text</a>
or
<a href=#ViewBag.check>You must register</a>

how to retrieve value of dropdown selection when form is not been submitted yet

I have following code
#using (Html.BeginForm())
{
#Html.DropDownListFor(m => m.IDs, new SelectList(Model. IDs), Model.SelectedID)
}
So user selection from this combo bind to SelectedID property of the model. My understanding is that this binding happen only when form is submitted. Let’s say from the same page, I need to do an AJAX call but at this point ) Model.SelectedID does not provide any value because form hasn’t been submitted yet (although user has selected something from drop down). Any ideas how to best deal with this situation?
You can use javascript.
var selectedValue = $("#IDs").val();
bind a change event to your DD
$("#DDL_ID").change(function(){
var currVal = $(this).val();
//do ajax
});
As others have pointed you would get this value with javascript on the change of the drop down list.
I wanted to point out however, that your understanding of the overload you are using for the drop down list is incorrect. This overload will display a default option box label.
For example you could prompt the users to select select something from the list:
#Html.DropDownListFor(m => m.IDs, new SelectList(Model. IDs), "Select Something...")
If you were to post the form in your example as is, you can see the selected item come across in the form. If your view model is setup in such a fashion the model binder would take over and bind this value to your "SelectedID" property.
[HttpPost]
public string DropDown(FormCollection form)
{
var selectedItem = form["IDs"];
return selectedItem;
}

Resources