MVC (MVC3) 检查模型是否存在或具有值

发布于 2024-12-01 12:57:13 字数 392 浏览 0 评论 0原文

我创建了一个工作 DropDownListFor ,它从 Model.IssueSocialSec 选择列表中获取数据,然后将来自数据库的值设置为 Model.SocialDBValue

但是,当我单击编辑链接时,查询存储库将模型传递回页面,这是可行的,但是如果我对页面执行了所需的重定向路由,并且没有任何东西可以绑定模型,那么页面就会失败。我将尝试让它传回一个空模型,但我想我会发布这个,因为我总是喜欢听到有关“最佳实践”和经验教训的反馈。

 @Html.DropDownListFor(m => m.SelectedSocial, new SelectList(Model.IssueSocialSec, "Value", "Text", Model.SocialDBValue), "") 

I have created a working DropDownListFor which gets the data from a selectlist which is Model.IssueSocialSec and then setting the value coming from the database is Model.SocialDBValue

However, when I click a Edit link which with query a repository passing the Model back to the page, that works, but if I do a needed redirect route to the page and nothing is there to bind the Model, then the page fails. I'm going to try having it pass back an empty Model, but I figured I would post this as I always like to hear feedback on "best practices" and lessons learned.

 @Html.DropDownListFor(m => m.SelectedSocial, new SelectList(Model.IssueSocialSec, "Value", "Text", Model.SocialDBValue), "") 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘和柠 2024-12-08 12:57:13

听起来您只需将 DropDownListFor 包装在

中,并使用指向允许您进行编辑的操作的 url。如果表单是幂等操作,则可以使用 GET 请求,并且当

一般来说,我构建 MVC 控制器和操作,以便

public class ProfilesController : Controller
{
    public IProfileRepository Profiles { get; private set; }

    public ProfilesController(IProfilesRepository profiles)
    {
        Profiles = profiles;
    }

    [HttpGet]
    public ActionResult Index()
    {
        var profiles = Profiles.All();

        return View(new ProfilesModel { Profiles = profiles });
    }

    [HttpGet]
    public ActionResult Edit(int id)
    {
        var profile = Profiles.GetById(id);

        return View(new ProfileModel { Profile = profile });
    }

    [HttpPost]
    public ActionResult Edit(ProfileModel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        var profile = Profiles.GetById(id);       

        // update the profile 
        Mapper.Map(model, profile);

        if (Profiles.Update(profile))
        {
            TempData["message"] = "Profile updated successfully";
        }

        return RedirectToAction("Edit");
    }
}

Index 将呈现所有配置文件。对于每个配置文件,将呈现一个 ,其中包含指向“编辑”的 URL,并且该 URL 将包含要编辑的配置文件的 idEdit 视图会将表单发布到 Edit,并且配置文件将根据模型的更改进行更新。

我建议查看类似 NerdDinnerMVC 音乐商店 了解他们如何构建代码。

It sounds like you just need to wrap the DropDownListFor in a <form> with a url pointing to an action that will allow you to edit. The form can use a GET request if it's an idempotent operation and you could use JavaScript to submit the form when the value of the <select> is changed, falling back to rendering a button for submission for when JavaScript is disabled.

Generally, I structure MVC controllers and actions as so

public class ProfilesController : Controller
{
    public IProfileRepository Profiles { get; private set; }

    public ProfilesController(IProfilesRepository profiles)
    {
        Profiles = profiles;
    }

    [HttpGet]
    public ActionResult Index()
    {
        var profiles = Profiles.All();

        return View(new ProfilesModel { Profiles = profiles });
    }

    [HttpGet]
    public ActionResult Edit(int id)
    {
        var profile = Profiles.GetById(id);

        return View(new ProfileModel { Profile = profile });
    }

    [HttpPost]
    public ActionResult Edit(ProfileModel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        var profile = Profiles.GetById(id);       

        // update the profile 
        Mapper.Map(model, profile);

        if (Profiles.Update(profile))
        {
            TempData["message"] = "Profile updated successfully";
        }

        return RedirectToAction("Edit");
    }
}

Index will render all the profiles. Against each profile, an <a> will be rendered with a URL pointing to Edit and the URL will include the id for the profile to edit. Edit view will post a form to Edit and the profile will be updated with changes from the model.

I recommend looking at something like NerdDinner or MVC Music store to get an idea of how they structure their code.

这样的小城市 2024-12-08 12:57:13

我最终像这样修复了它:

    ChildInfoModel childviewmodel = new ChildInfoModel();
    return View(childviewmodel);

在我尝试这样做之前:
返回View()

I ended up fixing it like this:

    ChildInfoModel childviewmodel = new ChildInfoModel();
    return View(childviewmodel);

before I was trying to just do:
return View()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文