Click here to Skip to main content
12,399,561 members (51,421 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET .NET Ajax MVC jQuery , +
Hi i came here with such unresolved issue.

I've ASP.NET MVC 4 web application with specific page which contains table of data,
(see below), this page simply contains a table with data, option set, and button to change table content according to value in option set:

<h2>
	Index</h2>
<p>
	@Html.ActionLink("Create New", "Create") 
</p>
<p>
<fieldset>
		Movie Type
		<select id="prePurchaseType" name="Type">
			<option value="0" selected="selected">Time to start</option>
			<option value="1">Highest prize value</option>
		</select>
		<input type="submit" id="btnSearch" value="Search" />
	</fieldset>
</p>
 
<table>
	<tr>
		<th>
			@Html.DisplayNameFor(model => model.RoomName)
		</th>
		<th>
			@Html.DisplayNameFor(model => model.TimeToStart)
		</th>
		<th>
			@Html.DisplayNameFor(model => model.PrizeValue)
		</th>
		<th>
		</th>
	</tr>
	@foreach (var item in Model)
 {
		<tr>
			<td>
				@Html.DisplayFor(modelItem => item.RoomName)
			</td>
			<td>
				@Html.DisplayFor(modelItem => item.TimeToStart)
			</td>
			<td>
				@Html.DisplayFor(modelItem => item.PrizeValue)
			</td>
			<td>
				@Html.ActionLink("Edit", "Edit", new { id = item.RoomId }) |
				@Html.ActionLink("Details", "Details", new { id = item.RoomId }) |
				@Html.ActionLink("Delete", "Delete", new { id = item.RoomId })
			</td>
		</tr>
 }
</table>
<script type="text/javascript" src="@Url.Content("~/Script/jquery-ui-1.8.11.min.js")" />
<script type="text/javascript">
	$(document).ready(function () {
		$('btnSearch').click(function () {
			var type = $("#prePurchaseType").val();
 
			$.ajax({
				type: "POST",
				contentType: "application/json; charset=utf-8",
				url: "Order/OrderByType",
				data: '{"type":"' + type + '"}',
				dataType: "json",
				success: function (msg) {
					$('.item_wrap').after('<div>' + msg.d + '</div>');
				},
				error: function (xhr, status, error) {
					var err = eval("(" + xhr.responseText + ")");
					alert(err.Message);
				}
			});
		});
	});
</script>

Controller method:
[HttpPost]
        public ActionResult Order(Models.PrePurchaseSearch search)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "Form is not valid! " +
                        "Please correct it and try again."
                    });
                }
                return Json(new { Result = "OK", Record = search });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
            return Index();
        }
where Models.PrePurchaseSearch is:

public class PrePurchaseSearch
    {
        public string Type { get; set; }
    }


Maybe someone can help me with this issue and rewrite my implementation in such way, that by selecting appropriate data in my select control and pressing btnSearch , application will change content (sort data by appropriate prePurchaseType)
??
Posted 14-Aug-12 23:03pm
Updated 14-Aug-12 23:04pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Seems like your Order method actually needs to do the search Smile | :) ... e.g. select from items where prePurchaseType=search (but obviously we don't know what your database looks like). And then pack it up into JSON since that seems to be what you're using for transfer.
  Permalink  
Comments
Oleksandr Kulchytskyi 15-Aug-12 10:57am
   
yep ,sorry for incorrect piece of code.

[HttpPost]
public ActionResult OrderByType(Models.PrePurchaseSearch search)
{
try
{
if (!ModelState.IsValid)
{
return Json(new
{
Result = "ERROR",
Message = "Form is not valid! " +
"Please correct it and try again."
});
}
return Json(db.RoomData).Where(x=>x.type==search.Type);
}
catch (Exception ex)
{
return Json(new { Result = "ERROR", Message = ex.Message });
}
return Index();
}

But with it code my $.post method fails, i dont know why.
In debug i go through code and ensures that query to db work properly and retreives appropriate data.
I work with EF 4.3.1 , and have chhosen Code-First approach.
BobJanova 15-Aug-12 11:06am
   
Define 'fail'.

Do you even have an OrderByType controller method (this is the URL that you're posting to)?
Oleksandr Kulchytskyi 15-Aug-12 11:23am
   
yes , of course , i'm a bit of careless today , so instead of name OrderByType i called it Order in method above.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Omg...
I think i have resolved this issue...
what i need, is just only set in construction of Json second property JsonRequestBehavior.AllowGet !!

return new JsonResult { /*set data here*/ , JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 15 Aug 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100