4

For example, I want to show a jQuery dialog after insert or edit a record in Action, in webform I can use the follow way to register js script to the page:

page.RegisterStartupScript(key, "<script language='javascript' defer>" + script + "</script>");

but this doesn't work in MVC, so how can I do this?

thanks!

2 Answers 2

3

In ASP.NET MVC scripts are not registered in controller actions. There's a separation between controllers and views. So you could directly put the script in the corresponding view:

<script type="text/javascript" src="somescript.js"></script>

When the view is rendered it will load the script. Now if you are doing AJAX and you want to add a script inside the success callback you could create the script element and append it to the DOM:

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'somescript.js';
$('#someElement').append(script);
Sign up to request clarification or add additional context in comments.

1 Comment

Your means I just write the js script to the view page? but how can I call this js function from the action? for example in my view page, I just need to call a jQuery dialog : <script type="text/javascript"> $(function() { $("#dialog").dialog("destroy"); }); </script>
0
static async register(req, res, next) {
    try {
        const { email, name, password } = req.body;
        if (!email) {
            throw {
                name: "Email is required",
            };
        }
        if (!name) {
            throw {
                name: "Name is required",
            };
        }
        if (!password) {
            throw {
                name: "Password is required",
            };
        }

        const user = await User.create({ email, password, name });

        res.status(201).json({
            id: user.id,
            name: user.name,
            email: user.email,
        });
    } catch (error) {
        next(error);
    }
}

1 Comment

Your answer could be improved by adding more information on what the code does and how it helps the OP.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.