locked
Using One View For All Controllers RRS feed

  • Question

  • User974100899 posted

    Hi - I am learning MVC and I want to always redirect to ~/Views/Home/index.cshtml

    This is the syntax I have for one Controller and it is trying to location ~/Views/EmpCheckController/index.cshtml which does not exist, because I don't want to redirect there...how do I need to alter code so I can ONLY use ~/Views/Home/index.cshtml

    [ApiController]
    [Route("api/EmpCheck")]
    public class EmpCheckController : Controller
    {
    	EmpCheck_context;
    
    	public EmpCheckController(TapCountContext context)
    	{
    		_context = context;
    	}
    
    	public IActionResult Index()
    	{
    		var VerifyAllEmpsOnDuty = _context.EmpCheckModel.FromSql("_GetClockedInEmps").ToList();
    		return View(VerifyAllEmpsOnDuty);
    	}
    }

    Friday, October 11, 2019 9:23 PM

Answers

  • User1034446946 posted

    assuming you have a standard asp.net mvc project, create a new controller not an api controller and mvc controller, copy your code over minus the api parts (the decorators) and debug it.

    or link the tutorial you used

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 14, 2019 1:50 AM

All replies

  • User475983607 posted

    Fundamentally, it is not possible to redirect to a View in MVC.  Actions return Views.  Either redirect to an Action or return a the View you want.  Use the following View() overload pattern.

    public IActionResult Index()
    {
    	var VerifyAllEmpsOnDuty = _context.TapCountModel.FromSql("_GetClockedInEmps").ToList();
    	return View("~/Views/TheFolder/Index.cshtml", VerifyAllEmpsOnDuty);
    }

    There's also creating a custom View Engine which you can just Google.

    The code shown looks like Web API which does not have a UI.  Can you clarify what you are trying to do?

    Friday, October 11, 2019 9:44 PM
  • User1034446946 posted

    api controllers don't return a view,so you don't need a view just return the response.

    Saturday, October 12, 2019 2:08 PM
  • User974100899 posted

    EnenDaveyBoy

    api controllers don't return a view,so you don't need a view just return the response.

    Well that explains why I was not getting my expected result returned.  I am expecting a JSON response...how do I get the response?

    What I'm after here is to return the results from the SQL Query of VerifyAllEmpsOnDuty to a table on my page located here: ~/Views/Home/Index.cshtml

    The below does not return an error in my code or the console, but my table is not populated with the results....

    iew("~/Views/TheFolder/Index.cshtml", VerifyAllEmpsOnDuty);
    }
    Sunday, October 13, 2019 3:04 AM
  • User475983607 posted

    Simply return the type as illustrated in any Web API tutorial.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

    Sunday, October 13, 2019 12:14 PM
  • User974100899 posted

    I have a Asp.net core MVC app that I have added a button to.  On the button press event I am using AJAX to query MS SQL Server and I want to return the stored procedure results in JSON format and display in a JQuery DataGridView.

    Following your suggestion above, I now get this prompt in the browser on button press event

    DataTables warning: table id=example - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3

    I do not understand what to do to remedy...if I need to show addditional code, pleas let me know.

    Sunday, October 13, 2019 5:20 PM
  • User475983607 posted

    Read the DataTable docs.

    https://datatables.net/reference/option/ajax

    Sunday, October 13, 2019 5:45 PM
  • User974100899 posted

    i've read it multiple times, but am not understanding  what I need to chnage in my code so that the table is populated wiht data.

    I am happy to show additional code if needed..

    Sunday, October 13, 2019 6:44 PM
  • User1034446946 posted

    what front end are you using?

    if your using a web api, you don't have views like you do in an asp.net mvc,look for something like react, angulr or vue,or even a static html site with ajax

    use postman to validate what the web api is sending

    console.log will show the obj you sending to the api,and building test will show its working as expected

    Sunday, October 13, 2019 8:52 PM
  • User974100899 posted

    I'm using ASP.Net Core & MVC to connect to a MS SQL Server Install and attempt to return data from it with .FromSql()

    I have verified that the data is being returned from MS SQL Server as expected with Console.Log() - I just am unsure how to have th eDataTable populated at this poitn as I get the error I listed above.

    Sunday, October 13, 2019 9:07 PM
  • User475983607 posted

    I have verified that the data is being returned from MS SQL Server as expected with Console.Log() - I just am unsure how to have th eDataTable populated at this poitn as I get the error I listed above.

    The DataTable documentation illustrate the data format DataTable expects.  Keep in mind, Forum members cannot see the HTML table you are trying to populate or your stored procedure result set.

    The DataTable documentation provides example code and example JSON.  I recommend  following the example code to learn how to use the DataTable library.  

    https://datatables.net/examples/data_sources/ajax.html

    You'll need to share enough code to reproduce the issue if you want the community to debug your code.

    Sunday, October 13, 2019 9:30 PM
  • User974100899 posted

    @mgebhard ->

    Below is more code, please let me know if I Need to show any additional....

    I am wanting to populate the table1 in my HTML with the data from my FromSQL

    This is my JavaScript
    $("#btnClick").click(function () {
        $('#example').DataTable({
            ajax: {
                url: getURL,
                method: "GET"
            },
            columns: [
                { data: "Location" },
                { data: "TC" }
            ]
        });
    });
    
    This is my HTML
    <div id="table1">
        <table id="example" class="display">
        </table>
    </div>
    <br />
    <div class="text-center">
        <div class="btn-group">
            <button type="button" id="btnClick">Button One</button>
    	</div>
    </div>
    @section Scripts {
        <script>
            var getURL = '@Url.Action("GTC")';
        </script>
    }
    
    Controller:
    [ApiController]
    [Route("api/EmpCheck")]
    public class EmpCheckController : Controller
    {
    	EmpCheck _context;
    
    	public EmpCheckController( context)
    	{
    		_context = context;
    	}
    
    	public IActionResult Index()
    	{
    		var VerifyAllEmpsOnDuty = _context.EmpCheckModel.FromSql("_GetClockedInEmps").ToList();
    		return View(VerifyAllEmpsOnDuty);
    	}
    }

    Sunday, October 13, 2019 11:21 PM
  • User1034446946 posted

    You can't achieve what your planning with your current setup, in MVC a normal controller will render the view and send it to the browser, a web api controller doesn't know anything about the view it just returns data, so nothing will render your view.

    Change your controller to a normal controller and send a model to the view, can be anything you want, normally the main data for the view (could be an empty model if you wanted but would be a waste of a web request), then if you want to use additional information one options would be to use ajax calls from also a normal controller (can be the same controller) within the sameproject which is set to return json (or similar), but you could also use Action helpers to call controllers before the view is sent to the user.

    Sunday, October 13, 2019 11:42 PM
  • User974100899 posted

    You can't achieve what your planning with your current setup, in MVC a normal controller will render the view and send it to the browser, a web api controller doesn't know anything about the view it just returns data, so nothing will render your view.

    Change your controller to a normal controller and send a model to the view, can be anything you want, normally the main data for the view (could be an empty model if you wanted but would be a waste of a web request), then if you want to use additional information one options would be to use ajax calls from also a normal controller (can be the same controller) within the sameproject which is set to return json (or similar), but you could also use Action helpers to call controllers before the view is sent to the user.

    Thank you for letting me know I am unable to do what I'm after with my current set-up.  Now I can stop the endless hours of googling trying to find out how to. :)

    Can you provide code to illustrate what you mean?  

    Sunday, October 13, 2019 11:57 PM
  • User1034446946 posted

    best i can do is off this link, it does everything you need to know to fix your problem

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

    although the link uses entity framework, which you can swap out later for adifferent database setup if you wish, but the basics are all there.

    Monday, October 14, 2019 12:45 AM
  • User974100899 posted

    I've followed multiple tutorials...

    I have my connection string set-up 

    I have set up models and Controllers

    I just can't get my DataTable to populate on my index.cshtml

    Monday, October 14, 2019 1:09 AM
  • User1034446946 posted

    assuming you have a standard asp.net mvc project, create a new controller not an api controller and mvc controller, copy your code over minus the api parts (the decorators) and debug it.

    or link the tutorial you used

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 14, 2019 1:50 AM