I tried to do a CRUD operation. Operation is that if user clicks on Edit from grid then data should be shown in the control that is below the grid. It means, grid and controls are in same page. Below is the code:
#Html.ActionLink("Edit", "Update", new { id=item.Country_ID })
In controller :
[HttpPost]
public ActionResult Update(TBL_Country tbl_country)
{
if (ModelState.IsValid)
{
db.Entry(tbl_country).State = EntityState.Modified;
db.SaveChanges();
}
return View(tbl_country);
}
But, it shows an error stating :
It does not have any views. How can I give the same page view again?
I am a beginner in MVC4.
You only need to write the View path, something like this
return View( "~/Views/ControllerName/ViewName.cshtml", tbl_country);
Related
In my MVC controller I have two action methods.
The first one is Index method:
public IActionResult Index()
{
return PopulateViewModel();
}
The "PopulateViewModel" Action Method is used for updating of the view model and then showing these updated values on the Index view.
public IActionResult PopulateViewModel()
{
ViewModel viewModel = new ViewModel()
{
//updating values in the view model
//the values are received when the form in the view is submitted
};
return View("Index", viewModel);
}
The problem that I have is that on my Index view the updated values are not shown immediately after submitting the form in the view. When I submit the form I must then once again refresh the page to see the updated values.
What could be the reason for such behavior and how can I correct that?
You misunderstand the conceptual notion. The index is supposed to represent the initial page state. Other actions within the controller will modify the output by rendering the page with the adjusted model. Or handling server side model binding, but the concept is fundamentally achieving the same result.
Your controller logic should be within the following constraints.
public class SampleController : Controller
{
public IActionResult Index() => new View("...", ...);
public IActionResult SubmitSample(string location)
{
var model = service.GetLabLocations(location);
return View("...", model);
}
}
The index is simulating a GET request, returning the initial page in the required state. The form portion, should POST data, outlined in the SubmitSample portion of the code. This will change the state of the page, but the server will need to render with those changes. So the page will load with the attached model, for you to display.
This would represent Razor more than likely on the server side.
#if(Model != null)
foreach(var sample in Model)
{
// Markup, with the data
}
I'm an MVC Newbie. I realize this is very basic stuff.
During entry of a Master Record, how do I get my MVC Controler action to redirect to a View for a secondary file that holds multiple records linked to the master record.
The scaffolding provides a view and controller to enter the master record but then takes you back to the Index View. I have tried various things in the controller to do this, but I'm striking out.
The tables are very simple and hold people and charges. One person, many charges.
I need to...
Enter new person record
Click Save
Navigate to view that allows entry of one or many charges that will be linked to the person.
Here is my Post in the controller.
[HttpPost]
public ActionResult Create(dataOffender offender)
{
if (ModelState.IsValid)
{
db.dataOffenders.Add(offender);
db.SaveChanges();
return RedirectToAction("Index");
<<<<<<<<<TODO:redirect to my charges "CreateCharge" view.
}
return View(offender);
}
Thanks for any help you can give.
You are redirecting to the Index after saving the new entry, instead you should redirect to the action CreateCharge.
[HttpPost]
public ActionResult Create(dataOffender offender)
{
if (ModelState.IsValid)
{
db.dataOffenders.Add(offender);
db.SaveChanges();
return RedirectToAction("CreateCharge", new KeyValuePair(offender.Key, offender.Value));
}
return View(offender);
}
public ActionResult CreateCharge(KeyValuePair kvp)
{
ViewBag.OffenderKeyValue = kvp;
return View();
}
In my mvc project i have a controller with the following actions:
public ActionResult Index()
{
return View(new List<Product>());
}
The corresponding index view render a partial view with the master grid:
#model System.Collections.Generic.List<TbbModels.Domain.Product>
#Html.Partial("_ProdutoMasterGrid", Model)
This partial view have a submit button and a grid. The client needs to put some data in the form and submit it to that action:
public ActionResult _ProdutoMasterGrid(string param)
{
return PartialView("_ProdutoMasterGrid",
repository.Compare(param).ToList());
}
But than i get the grid without the layout. How can i return a partial view with the layout?
You should explicitly return the correct view:
public ActionResult _ProdutoMasterGrid(string param)
{
return View("Index",
repository.Compare(param).ToList());
}
This ensures that when you do a post to this action, it returns the index view. I'm supposing here that repository.Compare return a List<Product> as well, since the type needs to match.
In an MVC4 project I need to "refresh" the page depending on some messages that can be present, otherwise I just redirect to a page, and if presenting again the page if them messages are present I would like to avoid just returning the View as it will cause then the double submission when the user tries to refresh it.
What I'm trying to do is this
[HttpGet]
public ActionResult SampleMethod()
{
viewModel = _builder.Build();
return View(viewModel);
}
[HttpPost]
public void SampleMethod(SampleViewModel viewModel)
{
if (ModelState.IsValid)
{
var response = serviceCall;
var errorMessages = response.ErrorMessages;
if (!errorMessages.Any())
{
//Redirect to proper view
}
else
vm = _builder.Build();
}
else vm = _builder.Build(); //There is some validation error I rebuild
CashbackOffersConfirmation(vm);
}
public ActionResult SampleMethodConfirmation(SampleViewModel viewModel)
{
return View("SampleMethod", viewModel);
}
It goes through the process
but the final page is .../SampleMethod instead of .../SampleMethodConfirmation and is blank,
Is this something to do with the routing (quite lost in this)? Is this a correct approach?
Thanks
In order to pass the object model from the view to the controller, you need to make a post request. Make sure you use a form that will generate the post request.
Also make the SampleMethodConfirmation method a post.
E.g.: add [HttpPost] on top of the method in the controller
I am fairly new to MVC and i'm looking for advice on how to setup a particular registration controller.
I have a controller called AccountController which has a Register method and I have a Register.cshtml.
Now, one of the biggest problems I seem stuck on is that I have 2 dropdowns that I need to populate based on the response from a service as these values change depending on location and other various parameters.
I have my page started and loading but I'm not sure what to do once a user click 'register'.
#model Adw.Models.RegisterModel //This is my model
#Html.DropDownListFor(m => m.States, new SelectList(Model.States)); // I load my dropdowns here
[AllowAnonymous]
public ActionResult Register(RegisterModel model)
{
model.States = Services.GetStates().Payload;
model.Countries = Services.GetCountries().Payload;
return View(model);
}
So my question is, when a user submits the form, should it come back to this same method? If so what would be the best way to validate that this is a submit rather than an initial load?
Also i haven't done much in the way of error handling and could use a suggestion on that, such as if either of the above service calls fail, then a registration cannot be completed, should that direct to a new page or is there a easy way to build that kind of error into the same page?
You should create two different method. One for GET and second for POST request:
[AllowAnonymous]
[HttpGet]
public ActionResult Register()
{
...
}
[AllowAnonymous]
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// create user
return this.RedirectToAction("SignIn");
}
else
{
return View(model);
}
}
You can review sample from default template.