I am new to MVC and this is my first attempt at creating a SurfaceController. It is a basic form with 2 dropdowns. Origin Port and Destination port. When the user selects an origin port then there is an ajax call that gets available destination ports.... All this is working until here.
When I submit the form I need to go to the Database and get all Records with the Origin Port and Destination Port that were selected. When I get to the HttpPost call "RateRequest" how do I send the values back to the cshtml page. The "GetAllFreightRates" call has records but the WebGrid does not bind with the new values.
I would greatly appreciate a kick in the right direction...
This is my surface Controller:
public class RateRequestSurfaceController: SurfaceController
{
[HttpPost]
public ActionResult RateRequest(RateRequestModel model)
{
////***** At this point only model.OiginPort and model.DestinationPorts have value.
if (ModelState.IsValid)
{
model.FreightRates = FreightRateServices.GetAllFreightRates();
}
return CurrentUmbracoPage();
}
[HttpPost]
public ActionResult BindDischargePorts(string originPortId)
{
try
{
var data = PortServices.GetAvailableDischargePorts(string.IsNullOrEmpty(originPortId) ? (int?)null : Convert.ToInt32(originPortId)).ToList();
data.Insert(0, new PortModel() { Name = "-- All --", Id = 0 });
return Json(new { ok = true, data, message = string.Empty });
}
catch (Exception ex)
{
return Json(new { ok = false, message = ex.Message });
}
}
[ChildActionOnly]
public ActionResult RenderActionResult()
{
var rateRequestmodel = new RateRequestModel
{
OriginPorts = PortServices.GetAvailableLoadingPorts().AsEnumerable().Select(x => new SelectListItem
{
Text = x.Name,
Value = x.Id.ToString()
}).ToList(),
DestinationPorts = PortServices.GetAvailableDischargePorts().AsEnumerable().Select(x => new SelectListItem
{
Text = x.Name,
Value = x.Id.ToString()
}).ToList(),
FreightRates = Enumerable.Empty<FreightRateModel>()
};
return PartialView("RateRequest", rateRequestmodel);
}
}
My Model:
public class RateRequestModel
{
[DisplayName("Origin Port")]
public IEnumerable<SelectListItem> OriginPorts { get; set; }
public int OriginPortId { get; set; }
[DisplayName("Destination Port")]
public IEnumerable<SelectListItem> DestinationPorts { get; set; }
public int DestinationPortId { get; set; }
public IEnumerable<FreightRateModel> FreightRates { get; set; }
Surface Controller With Grid
I am new to MVC and this is my first attempt at creating a SurfaceController. It is a basic form with 2 dropdowns. Origin Port and Destination port. When the user selects an origin port then there is an ajax call that gets available destination ports.... All this is working until here.
When I submit the form I need to go to the Database and get all Records with the Origin Port and Destination Port that were selected. When I get to the HttpPost call "RateRequest" how do I send the values back to the cshtml page. The "GetAllFreightRates" call has records but the WebGrid does not bind with the new values.
I would greatly appreciate a kick in the right direction...
This is my surface Controller:
My Model:
public class RateRequestModel {
}
cshtml:
I ended up passing the data in the ViewData, because I could not figure out how to pass the data back using the Model.
is working on a reply...