Getting Error While Using ActionResult - asp.net-mvc

I have a View(FilerOverall) and inside the view i am calling some method using renderaction method.
#{
Html.RenderAction("Gettemplate", "FinancialDisclosure",
new { FormId = "100",ScheduleId= "10" });
};
and in controller i have written the action method like
public ActionResult Gettemplate(string FormId ,string ScheduleId)
{
List<FDDTO> FD1 = FDService.GetScheduleDetails(100, 10).ToList();
return View ("EditorTemplates/FDDTO", FD1);
}
when I executed the app I am getting this error:
"A public action method 'Gettemplate' was not found on controller
'WorldBank.DOI.Presentation.Controllers.FinancialDisclosureController'."}

Try #Html.Action instead of Html.RenderAction
#Html.Action("Gettemplate", "FinancialDisclosure", new { FormId = "100",ScheduleId= "10" })

you should try this
Create new view like Demo.cshtml
now
public ActionResult Gettemplate(string FormId ,string ScheduleId)
{
List<FDDTO> FD1 = FDService.GetScheduleDetails(100, 10).ToList();
return View ("Demo", FD1);
}
now put in your FilerOverall.cshtml below code
#{
Html.RenderAction("Gettemplate", "FinancialDisclosure",
new { FormId = "100",ScheduleId= "10" });
};

Related

unwanted query string in RedirectToAction

I am using redirect statement like below to go to specific action with parameter
return RedirectToAction(nameof(ActivityTypeController.Create), "ActivityType", new { selectedID = 34 });
and my action route configuration is
[Route("Create/{selectedID}")]
public IActionResult Create(int selectedID)
what I expected is
http://localhost:27945/ActivityType/Create/34
but it return
http://localhost:27945/ActivityType/Create?selectedID=34
I also use
RouteData.Values.Remove("selectedID")
but nothing changed!
I am using MVC6 with Asp.net 5 Template
Updated:
my redirected action is like below
[Route("Create/{selectedID}")]
public IActionResult Create(int selectedID)
{
BL.BO.ActivityTypeBO bo = new BL.BO.ActivityTypeBO();
ViewBag.Title = "title1";
ViewBag.Description = "some desc";
var data = bo.GetAll().Where(p => p.ParentID == null).OrderBy(p => p.Title);
SelectList parents;
parents = new SelectList(data, "ID", "Title", selectedID);
ViewBag.Parents = parents;
return View();
}
and I use RoutDate just before redirect to action
RouteData.Values.Remove("selectedID");
return RedirectToAction(nameof(ActivityTypeController.Create), "ActivityType", new { selectedID = 34 });
MVC will add the parameters like a query string.So, in cases like yours, you need to append it to the action param of RedirectToAction method.Below is the code
return RedirectToAction(nameof(ActivityTypeController.Create) + "/34" , "ActivityType");

How to pass model to partialview at run time?

ViewModel
public class ModelTypeViewModel
{
public virtual CheckRadioButton CRB { get; set; }
}
Controller
public class M1Controller : Controller
{
public CarContext db = new CarContext();
private CheckRadioButton get()
{
CheckRadioButton c = new CheckRadioButton();
c.BrandName = "abc";
c.type = "xyz";
return c;
}
public ActionResult Hello ()
{
CheckRadioButton s = get();
ModelTypeViewModel mm = new ModelTypeViewModel(s);
return View(mm);
}
View:(Hello)
#model Car.Models.ModelTypeViewModel
#Html.Partial("_Display", Model.CRB)
Partial View(_Display)
<h1> Hello </h1>
How can I pass diff model each time to to partial view?
It gives an error
"An exception of type 'System.Web.HttpParseException' occurred in System.Web.WebPages.Razor.dll but was not handled in user code"
It gives the same error even if I pass only 'Model"
I am confused
Put each button inside Ajax.BeginForm
#using (Ajax.BeginForm("BuyItem", "MsmqTest"}, new AjaxOptions { UpdateTargetId = "msmqpartial" }))
{ <button type="submit">Buy</button>}
#using (Ajax.BeginForm("BuyItem", "MsmqTest" }, new AjaxOptions { UpdateTargetId = "msmqpartial" }))
{
<button type="submit">Sell</button>
}
Where "updateTargetId" is the div id to append content
public ActionResult BuyItem()
{
if(//some condition goes here)
return PartialView("Partial1",data);
if(//some condition goes here)
return PartialView("Partial2",data);
}

How to pass a list in a get request

I have an url that works for passing a List of strings
/Home/Index?Person%5B0%5D=Myname&Person%5B1%5D=Yourname
Unencoded it is
/Home/Index?Person[0]=Myname&Person[1]=Yourname
The Action Method is
public ActionResult(List<string> person)
{
...
}
The Parameter List person will be correctly filled with the values Myname and Yourname.
I need to redirect to this url using RedirectToAction
I would usually do
RedirectToAction("Index","Home",new {Parameter1=value1})
But obviously I cant use Person%5B0%5D as a parameter name, because it has ivalid characters.
How can I create such a link or should I use a different URL - scheme?
hi i worked on your query and finally got the result just check this code find weather it work with your query.
Controller code :
public ActionResult showString()
{
try
{
IEnumerable<string> persons = new[] { "myname", "urname" };
var values = new RouteValueDictionary(
persons
.Select((sampleper, index) => new {sampleper, index })
.ToDictionary(
key => string.Format("[{0}]", key.index),
value => (object)value.sampleper
)
);
return RedirectToAction("details", values);
}
catch (Exception)
{
throw;
}
}
and another action method is details with list as parameter
public ActionResult details(IEnumerable<string> persons)
{
ViewBag.person = persons;
return View();
}
it also works if you pass the link as
http://localhost:2266/Home/details?%5B0%5D=myname&%5B1%5D=urname
the view of details action method is
#{
ViewBag.Title = "details";
}
<h2>details</h2>
<ul>
#foreach (var i in ViewBag.person)
{
<li>#i</li>
}
</ul>

MVC3 error when trying to render action as partial view

I'm getting the following error when trying to use Html.Action to render a page as a partial view:
Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper
View code:
#Html.Action("Index", "Location", new { userId = 3428 })
Controller code:
public class Location : Controller
{
private void PopulateDropdowns(LocationViewModel model, int userId)
{
var lookup = new AWS.BL.Lookup();
model.Areas = lookup.GetAreas(userId);
model.SEATs = new List<DTO.Lookup>();
model.Establishments = new List<DTO.Lookup>();
model.Properties = new List<DTO.Lookup>();
}
public ActionResult Index(int userId)
{
var model = new LocationViewModel();
PopulateDropdowns(model, userId);
return PartialView(model);
}
}
Any help greatly appreciated.

View doesn't refresh after RedirectToAction is done

Here is my problem:
[HttpPost]
public ActionResult AddData(CandidateViewModel viewModel)
{
var newCandidateId = 0;
newCandidateId = this._serviceClient.AddCandidate(viewModel);
return RedirectToAction("DisplayCandidate",new {id=newCandidateId});
}
public ActionResult DisplayCandidate(int id)
{
var candidateViewModel= this._serviceClient.GetCandidate(id);
return View(candidateViewModel);
}
After filling the form viwemodel sends to server. After data were stored, flow is redirected to DisplayCandidate action and it goes there but page didn't refresh. I don't understand why! Help, please.
Because you are using Ajax Post
public ActionResult AddData(CandidateViewModel viewModel)
{
var newCandidateId = 0;
newCandidateId = this._serviceClient.AddCandidate(viewModel);
string ReturnURL = "/DisplayCandidate/"+newCandidateId;
return JSON(ReturnURL);
}
and in your Ajax Post Method:
Onsuccess(function(retURL){ window.location(retURL); })
This will take to the new Action and that Action will return View.
If you're using Ajax, return a script results to execute the navigation
instead of
return RedirectToAction("DisplayCandidate",new {id=newCandidateId});
try
var viewName = "/Path/ViewName";
var id = 1;
var urlNavigate = string.Format("location.href='{0}?id={1}'", viewName, id);
return new JavaScriptResult() { Script = urlNavigate };

Resources