Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

HTML
<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:
C#
[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:

C#
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
Updated 14-Aug-12 23:04pm
v2

Seems like your Order method actually needs to do the search :) ... 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.
 
Share this answer
 
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.
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 };
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900