Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3489044
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:20:43+00:00 2026-05-18T11:20:43+00:00

Updated I have recently modified a view to post to its controller a json

  • 0

Updated

I have recently modified a view to post to its controller a json object rather than a formcollection.

In order to get the unit test for this controller working we set the formvalue provider to a dictionary object to stop the UpdateModel method from throwing nre’s.

The resulting unit test below however, simply does not feel like the right thing to be doing. Any insight into how to rework this would be greatly appreciated.

    [HttpPost]
    public ActionResult ThemeContent(content model)
    {
        if (ModelState.IsValid)
        {
            var content = _contentRepository.GetContent(model.id);
            if (content == null)
            {
                content = new content();
                UpdateModel(content);
                _contentRepository.Add(content);
                _contentRepository.Save();
            }
            else
            {
                UpdateModel(content);
                _contentRepository.Save();
            }

            return Json(new
            {
                redirectUrl = Url.Action("index", "success", new {id = content.id}),
                isRedirect = true
            });
        }

        string errorMessage = "{";

        foreach (var key in ModelState.Keys)
        {
            var error = ModelState[key].Errors.FirstOrDefault();
            if (error != null)
            {
                if (errorMessage != "{")
                {
                    errorMessage += ",";
                }
                errorMessage += (char) 34 + "#" + key + (char) 34 + ":" + (char) 34 + error.ErrorMessage + (char) 34;
            }
        }

        errorMessage += "}";

        return Json(new
        {
            Message = errorMessage,
            isRedirect = false
        });

    }

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:20:43+00:00Added an answer on May 18, 2026 at 11:20 am

    Updated, as we were incorrectly using the updatemodel method.

           //setup
            var fakeContent = new content
            {
                address1 = "123 test street"
            };
    
            _controller.Url = new UrlHelper(
                new RequestContext(
                    _controller.HttpContext, new RouteData()
                    ),
                new RouteCollection()
                );
    
            //execute
            var result = _controller.ThemeContent(fakeContent) as JsonResult();
    
            //assert
            Assert.AreEqual("123 test street", _content.address1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have updated my jSon object using the code <script type=text/javascript > jQuery(document).ready(function($){ var
I have recently updated my ADT and it is now ADT_V20. I have also
I have recently updated my model, added a BooleanField to it however when I
I have recently updated the Cucumber gems ('cucumber' and 'cucumber-rails') on my machine and
I have recently installed iOS 6 beta on my device and updated to the
I recently updated from MVC4 beta to the RC and have encountered a small
I recently updated a rails app from 3.0.4 to 3.2.4 and I have a
Recently I updated my computer to Ubuntu 11.10 64-bits. I have a problem when
I have a rails environment which I set-up with macports. I recently updated macports
I have recently updated Xcode to use the iOS5 SDK. Since updating my app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.